packetizer: hevc: add poc debug
[vlc.git] / modules / codec / dvbsub.c
blob2040b90c77ceb62164ec2fbdde2b0449e753b68b
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 int 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( "spu 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 = 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_codec = 0;
372 default_clut_init( p_dec );
374 return VLC_SUCCESS;
377 /*****************************************************************************
378 * Close:
379 *****************************************************************************/
380 static void Close( vlc_object_t *p_this )
382 decoder_t *p_dec = (decoder_t*) p_this;
383 decoder_sys_t *p_sys = p_dec->p_sys;
385 var_Destroy( p_this, DVBSUB_CFG_PREFIX "x" );
386 var_Destroy( p_this, DVBSUB_CFG_PREFIX "y" );
387 var_Destroy( p_this, DVBSUB_CFG_PREFIX "position" );
389 free_all( p_dec );
390 free( p_sys );
393 /*****************************************************************************
394 * Flush:
395 *****************************************************************************/
396 static void Flush( decoder_t *p_dec )
398 decoder_sys_t *p_sys = p_dec->p_sys;
400 p_sys->i_pts = VLC_TS_INVALID;
403 /*****************************************************************************
404 * Decode:
405 *****************************************************************************/
406 static int Decode( decoder_t *p_dec, block_t *p_block )
408 decoder_sys_t *p_sys = p_dec->p_sys;
410 if( p_block == NULL ) /* No Drain */
411 return VLCDEC_SUCCESS;
413 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED) )
415 Flush( p_dec );
416 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
418 block_Release( p_block );
419 return VLCDEC_SUCCESS;
423 /* configure for SD res in case DDS is not present */
424 /* a change of PTS is a good indication we must get a new DDS */
425 if( p_sys->i_pts != p_block->i_pts )
426 default_dds_init( p_dec );
428 p_sys->i_pts = p_block->i_pts;
429 if( p_sys->i_pts <= VLC_TS_INVALID )
431 #ifdef DEBUG_DVBSUB
432 /* Some DVB channels send stuffing segments in non-dated packets so
433 * don't complain too loudly. */
434 msg_Warn( p_dec, "non dated subtitle" );
435 #endif
436 block_Release( p_block );
437 return VLCDEC_SUCCESS;
440 bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
442 if( bs_read( &p_sys->bs, 8 ) != 0x20 ) /* Data identifier */
444 msg_Dbg( p_dec, "invalid data identifier" );
445 block_Release( p_block );
446 return VLCDEC_SUCCESS;
449 if( bs_read( &p_sys->bs, 8 ) ) /* Subtitle stream id */
451 msg_Dbg( p_dec, "invalid subtitle stream id" );
452 block_Release( p_block );
453 return VLCDEC_SUCCESS;
456 #ifdef DEBUG_DVBSUB
457 msg_Dbg( p_dec, "subtitle packet received: %"PRId64, p_sys->i_pts );
458 #endif
460 p_sys->b_page = false;
461 while( bs_show( &p_sys->bs, 8 ) == 0x0f ) /* Sync byte */
463 decode_segment( p_dec, &p_sys->bs );
466 if( ( bs_read( &p_sys->bs, 8 ) & 0x3f ) != 0x3f ) /* End marker */
468 msg_Warn( p_dec, "end marker not found (corrupted subtitle ?)" );
469 block_Release( p_block );
470 return VLCDEC_SUCCESS;
473 /* Check if the page is to be displayed */
474 if( p_sys->p_page && p_sys->b_page )
476 subpicture_t *p_spu = render( p_dec );
477 if( p_spu != NULL )
478 decoder_QueueSub( p_dec, p_spu );
481 block_Release( p_block );
483 return VLCDEC_SUCCESS;
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 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 )
768 decoder_sys_t *p_sys = p_dec->p_sys;
769 int i_version, i_state, i_segment_length, i_timeout, i;
771 /* A page is composed by 0 or more region */
772 i_segment_length = bs_read( s, 16 );
773 i_timeout = bs_read( s, 8 );
774 i_version = bs_read( s, 4 );
775 i_state = bs_read( s, 2 );
776 bs_skip( s, 2 ); /* Reserved */
778 if( i_state == DVBSUB_PCS_STATE_CHANGE )
780 /* End of an epoch, reset decoder buffer */
781 #ifdef DEBUG_DVBSUB
782 msg_Dbg( p_dec, "page composition mode change" );
783 #endif
784 free_all( p_dec );
786 else if( !p_sys->p_page && ( i_state != DVBSUB_PCS_STATE_ACQUISITION ) &&
787 ( i_state != DVBSUB_PCS_STATE_CHANGE ) )
789 /* Not a full PCS, we need to wait for one */
790 msg_Dbg( p_dec, "didn't receive an acquisition page yet" );
792 #if 0
793 /* Try to start decoding even without an acquisition page */
794 bs_skip( s, 8 * (i_segment_length - 2) );
795 return;
796 #endif
799 #ifdef DEBUG_DVBSUB
800 if( i_state == DVBSUB_PCS_STATE_ACQUISITION )
801 msg_Dbg( p_dec, "acquisition page composition" );
802 #endif
804 /* Check version number */
805 if( p_sys->p_page && ( p_sys->p_page->i_version == i_version ) )
807 bs_skip( s, 8 * (i_segment_length - 2) );
808 return;
810 else if( p_sys->p_page )
812 if( p_sys->p_page->i_region_defs )
813 free( p_sys->p_page->p_region_defs );
814 p_sys->p_page->p_region_defs = NULL;
815 p_sys->p_page->i_region_defs = 0;
818 if( !p_sys->p_page )
820 #ifdef DEBUG_DVBSUB
821 msg_Dbg( p_dec, "new page" );
822 #endif
823 /* Allocate a new page */
824 p_sys->p_page = malloc( sizeof(dvbsub_page_t) );
825 if( !p_sys->p_page )
826 return;
829 p_sys->p_page->i_version = i_version;
830 p_sys->p_page->i_timeout = i_timeout;
831 p_sys->b_page = true;
833 /* Number of regions */
834 p_sys->p_page->i_region_defs = (i_segment_length - 2) / 6;
836 if( p_sys->p_page->i_region_defs == 0 ) return;
838 p_sys->p_page->p_region_defs =
839 vlc_alloc( p_sys->p_page->i_region_defs, sizeof(dvbsub_regiondef_t) );
840 if( p_sys->p_page->p_region_defs )
842 for( i = 0; i < p_sys->p_page->i_region_defs; i++ )
844 p_sys->p_page->p_region_defs[i].i_id = bs_read( s, 8 );
845 bs_skip( s, 8 ); /* Reserved */
846 p_sys->p_page->p_region_defs[i].i_x = bs_read( s, 16 );
847 p_sys->p_page->p_region_defs[i].i_y = bs_read( s, 16 );
849 #ifdef DEBUG_DVBSUB
850 msg_Dbg( p_dec, "page_composition, region %i (%i,%i)",
851 i, p_sys->p_page->p_region_defs[i].i_x,
852 p_sys->p_page->p_region_defs[i].i_y );
853 #endif
858 static void decode_region_composition( decoder_t *p_dec, bs_t *s )
860 decoder_sys_t *p_sys = p_dec->p_sys;
861 dvbsub_region_t *p_region, **pp_region = &p_sys->p_regions;
862 int i_segment_length, i_processed_length, i_id, i_version;
863 int i_width, i_height, i_level_comp, i_depth, i_clut;
864 int i_8_bg, i_4_bg, i_2_bg;
865 bool b_fill;
867 i_segment_length = bs_read( s, 16 );
868 i_id = bs_read( s, 8 );
869 i_version = bs_read( s, 4 );
871 /* Check if we already have this region */
872 for( p_region = p_sys->p_regions; p_region != NULL;
873 p_region = p_region->p_next )
875 pp_region = &p_region->p_next;
876 if( p_region->i_id == i_id ) break;
879 /* Check version number */
880 if( p_region && ( p_region->i_version == i_version ) )
882 bs_skip( s, 8 * (i_segment_length - 1) - 4 );
883 return;
886 if( !p_region )
888 #ifdef DEBUG_DVBSUB
889 msg_Dbg( p_dec, "new region: %i", i_id );
890 #endif
891 p_region = *pp_region = calloc( 1, sizeof(dvbsub_region_t) );
892 if( !p_region )
893 return;
894 p_region->p_object_defs = NULL;
895 p_region->p_pixbuf = NULL;
896 p_region->p_next = NULL;
899 /* Region attributes */
900 p_region->i_id = i_id;
901 p_region->i_version = i_version;
902 b_fill = bs_read( s, 1 );
903 bs_skip( s, 3 ); /* Reserved */
905 i_width = bs_read( s, 16 );
906 i_height = bs_read( s, 16 );
907 #ifdef DEBUG_DVBSUB
908 msg_Dbg( p_dec, " width=%d height=%d", i_width, i_height );
909 #endif
910 i_level_comp = bs_read( s, 3 );
911 i_depth = bs_read( s, 3 );
912 bs_skip( s, 2 ); /* Reserved */
913 i_clut = bs_read( s, 8 );
915 i_8_bg = bs_read( s, 8 );
916 i_4_bg = bs_read( s, 4 );
917 i_2_bg = bs_read( s, 2 );
918 bs_skip( s, 2 ); /* Reserved */
920 /* Free old object defs */
921 while( p_region->i_object_defs )
922 free( p_region->p_object_defs[--p_region->i_object_defs].psz_text );
924 free( p_region->p_object_defs );
925 p_region->p_object_defs = NULL;
927 /* Extra sanity checks */
928 if( ( p_region->i_width != i_width ) ||
929 ( p_region->i_height != i_height ) )
931 if( p_region->p_pixbuf )
933 msg_Dbg( p_dec, "region size changed (%dx%d->%dx%d)",
934 p_region->i_width, p_region->i_height, i_width, i_height );
935 free( p_region->p_pixbuf );
938 p_region->p_pixbuf = xmalloc( i_height * i_width );
939 p_region->i_depth = 0;
940 b_fill = true;
942 if( p_region->i_depth &&
943 ( ( p_region->i_depth != i_depth ) ||
944 ( p_region->i_level_comp != i_level_comp ) ||
945 ( p_region->i_clut != i_clut) ) )
947 msg_Dbg( p_dec, "region parameters changed (not allowed)" );
950 /* Erase background of region */
951 if( b_fill )
953 int i_background = ( i_depth == 1 ) ? i_2_bg :
954 ( ( i_depth == 2 ) ? i_4_bg : i_8_bg );
955 memset( p_region->p_pixbuf, i_background, i_width * i_height );
958 p_region->i_width = i_width;
959 p_region->i_height = i_height;
960 p_region->i_level_comp = i_level_comp;
961 p_region->i_depth = i_depth;
962 p_region->i_clut = i_clut;
964 /* List of objects in the region */
965 i_processed_length = 10;
966 while( i_processed_length < i_segment_length )
968 dvbsub_objectdef_t *p_obj;
970 /* We create a new object */
971 p_region->i_object_defs++;
972 p_region->p_object_defs = xrealloc( p_region->p_object_defs,
973 sizeof(dvbsub_objectdef_t) * p_region->i_object_defs );
975 /* We parse object properties */
976 p_obj = &p_region->p_object_defs[p_region->i_object_defs - 1];
977 p_obj->i_id = bs_read( s, 16 );
978 p_obj->i_type = bs_read( s, 2 );
979 bs_skip( s, 2 ); /* Provider */
980 p_obj->i_x = bs_read( s, 12 );
981 bs_skip( s, 4 ); /* Reserved */
982 p_obj->i_y = bs_read( s, 12 );
983 p_obj->psz_text = NULL;
985 i_processed_length += 6;
987 if( ( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ) ||
988 ( p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING ) )
990 p_obj->i_fg_pc = bs_read( s, 8 );
991 p_obj->i_bg_pc = bs_read( s, 8 );
992 i_processed_length += 2;
997 /* ETSI 300 743 [7.2.1] */
998 static void decode_display_definition( decoder_t *p_dec, bs_t *s )
1000 decoder_sys_t *p_sys = p_dec->p_sys;
1001 uint16_t i_segment_length;
1002 uint16_t i_processed_length = 40;
1003 int i_version;
1005 i_segment_length = bs_read( s, 16 );
1006 i_version = bs_read( s, 4 );
1008 /* Check version number */
1009 if( p_sys->display.i_version == i_version )
1011 /* The definition did not change */
1012 bs_skip( s, 8*i_segment_length - 4 );
1013 return;
1016 #ifdef DEBUG_DVBSUB
1017 msg_Dbg( p_dec, "new display definition: %i", i_version );
1018 #endif
1020 /* We don't have this version of the display definition: Parse it */
1021 p_sys->display.i_version = i_version;
1022 p_sys->display.b_windowed = bs_read( s, 1 );
1023 bs_skip( s, 3 ); /* Reserved bits */
1024 p_sys->display.i_width = bs_read( s, 16 )+1;
1025 p_sys->display.i_height = bs_read( s, 16 )+1;
1027 if( p_sys->display.b_windowed )
1029 #ifdef DEBUG_DVBSUB
1030 msg_Dbg( p_dec, "display definition with offsets (windowed)" );
1031 #endif
1032 /* Coordinates are measured from the top left corner */
1033 p_sys->display.i_x = bs_read( s, 16 );
1034 p_sys->display.i_max_x = bs_read( s, 16 );
1035 p_sys->display.i_y = bs_read( s, 16 );
1036 p_sys->display.i_max_y = bs_read( s, 16 );
1037 i_processed_length += 64;
1039 else
1041 /* if not windowed, setup the window variables to good defaults */
1042 /* not necessary, but to avoid future confusion.. */
1043 p_sys->display.i_x = 0;
1044 p_sys->display.i_max_x = p_sys->display.i_width-1;
1045 p_sys->display.i_y = 0;
1046 p_sys->display.i_max_y = p_sys->display.i_height-1;
1049 if( i_processed_length != i_segment_length*8 )
1051 msg_Err( p_dec, "processed length %d bytes != segment length %d bytes",
1052 i_processed_length / 8 , i_segment_length );
1055 #ifdef DEBUG_DVBSUB
1056 msg_Dbg( p_dec, "version: %d, width: %d, height: %d",
1057 p_sys->display.i_version, p_sys->display.i_width, p_sys->display.i_height );
1058 if( p_sys->display.b_windowed )
1059 msg_Dbg( p_dec, "xmin: %d, xmax: %d, ymin: %d, ymax: %d",
1060 p_sys->display.i_x, p_sys->display.i_max_x, p_sys->display.i_y, p_sys->display.i_max_y );
1061 #endif
1064 static void dvbsub_render_pdata( decoder_t *, dvbsub_region_t *, int, int,
1065 uint8_t *, int );
1066 static void dvbsub_pdata2bpp( bs_t *, uint8_t *, int, int * );
1067 static void dvbsub_pdata4bpp( bs_t *, uint8_t *, int, int * );
1068 static void dvbsub_pdata8bpp( bs_t *, uint8_t *, int, int * );
1070 static void decode_object( decoder_t *p_dec, bs_t *s )
1072 decoder_sys_t *p_sys = p_dec->p_sys;
1073 dvbsub_region_t *p_region;
1074 int i_segment_length, i_coding_method, i_id, i;
1076 /* ETSI 300-743 paragraph 7.2.4
1077 * sync_byte, segment_type and page_id have already been processed.
1079 i_segment_length = bs_read( s, 16 );
1080 i_id = bs_read( s, 16 );
1081 bs_skip( s, 4 ); /* version */
1082 i_coding_method = bs_read( s, 2 );
1084 if( i_coding_method > 1 )
1086 msg_Dbg( p_dec, "unknown DVB subtitling coding %d is not handled!", i_coding_method );
1087 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
1088 return;
1091 /* Check if the object needs to be rendered in at least one
1092 * of the regions */
1093 for( p_region = p_sys->p_regions; p_region != NULL;
1094 p_region = p_region->p_next )
1096 for( i = 0; i < p_region->i_object_defs; i++ )
1097 if( p_region->p_object_defs[i].i_id == i_id ) break;
1099 if( i != p_region->i_object_defs ) break;
1101 if( !p_region )
1103 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
1104 return;
1107 #ifdef DEBUG_DVBSUB
1108 msg_Dbg( p_dec, "new object: %i", i_id );
1109 #endif
1111 bs_skip( s, 1 ); /* non_modify_color */
1112 bs_skip( s, 1 ); /* Reserved */
1114 if( i_coding_method == 0x00 )
1116 int i_topfield, i_bottomfield;
1117 uint8_t *p_topfield, *p_bottomfield;
1119 i_topfield = bs_read( s, 16 );
1120 i_bottomfield = bs_read( s, 16 );
1121 p_topfield = s->p_start + bs_pos( s ) / 8;
1122 p_bottomfield = p_topfield + i_topfield;
1124 bs_skip( s, 8 * (i_segment_length - 7) );
1126 /* Sanity check */
1127 if( ( i_segment_length < ( i_topfield + i_bottomfield + 7 ) ) ||
1128 ( ( p_topfield + i_topfield + i_bottomfield ) > s->p_end ) )
1130 msg_Dbg( p_dec, "corrupted object data" );
1131 return;
1134 for( p_region = p_sys->p_regions; p_region != NULL;
1135 p_region = p_region->p_next )
1137 for( i = 0; i < p_region->i_object_defs; i++ )
1139 if( p_region->p_object_defs[i].i_id != i_id ) continue;
1141 dvbsub_render_pdata( p_dec, p_region,
1142 p_region->p_object_defs[i].i_x,
1143 p_region->p_object_defs[i].i_y,
1144 p_topfield, i_topfield );
1146 if( i_bottomfield )
1148 dvbsub_render_pdata( p_dec, p_region,
1149 p_region->p_object_defs[i].i_x,
1150 p_region->p_object_defs[i].i_y + 1,
1151 p_bottomfield, i_bottomfield );
1153 else
1155 /* Duplicate the top field */
1156 dvbsub_render_pdata( p_dec, p_region,
1157 p_region->p_object_defs[i].i_x,
1158 p_region->p_object_defs[i].i_y + 1,
1159 p_topfield, i_topfield );
1164 else
1166 /* DVB subtitling as characters */
1167 int i_number_of_codes = bs_read( s, 8 );
1168 uint8_t* p_start = s->p_start + bs_pos( s ) / 8;
1170 /* Sanity check */
1171 if( ( i_segment_length < ( i_number_of_codes*2 + 4 ) ) ||
1172 ( ( p_start + i_number_of_codes*2 ) > s->p_end ) )
1174 msg_Dbg( p_dec, "corrupted object data" );
1175 return;
1178 for( p_region = p_sys->p_regions; p_region != NULL;
1179 p_region = p_region->p_next )
1181 for( i = 0; i < p_region->i_object_defs; i++ )
1183 int j;
1185 if( p_region->p_object_defs[i].i_id != i_id ) continue;
1187 p_region->p_object_defs[i].psz_text =
1188 xrealloc( p_region->p_object_defs[i].psz_text,
1189 i_number_of_codes + 1 );
1191 /* FIXME 16bits -> char ??? See Preamble */
1192 for( j = 0; j < i_number_of_codes; j++ )
1194 p_region->p_object_defs[i].psz_text[j] = (char)(bs_read( s, 16 ) & 0xFF);
1196 /* Null terminate the string */
1197 p_region->p_object_defs[i].psz_text[j] = 0;
1202 #ifdef DEBUG_DVBSUB
1203 msg_Dbg( p_dec, "end object: %i", i_id );
1204 #endif
1207 static void dvbsub_render_pdata( decoder_t *p_dec, dvbsub_region_t *p_region,
1208 int i_x, int i_y,
1209 uint8_t *p_field, int i_field )
1211 uint8_t *p_pixbuf;
1212 int i_offset = 0;
1213 bs_t bs;
1215 /* Sanity check */
1216 if( !p_region->p_pixbuf )
1218 msg_Err( p_dec, "region %i has no pixel buffer!", p_region->i_id );
1219 return;
1221 if( i_y < 0 || i_x < 0 || i_y >= p_region->i_height ||
1222 i_x >= p_region->i_width )
1224 msg_Dbg( p_dec, "invalid offset (%i,%i)", i_x, i_y );
1225 return;
1228 p_pixbuf = p_region->p_pixbuf + i_y * p_region->i_width;
1229 bs_init( &bs, p_field, i_field );
1231 while( !bs_eof( &bs ) )
1233 /* Sanity check */
1234 if( i_y >= p_region->i_height ) return;
1236 switch( bs_read( &bs, 8 ) )
1238 case 0x10:
1239 dvbsub_pdata2bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1240 &i_offset );
1241 break;
1243 case 0x11:
1244 dvbsub_pdata4bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1245 &i_offset );
1246 break;
1248 case 0x12:
1249 dvbsub_pdata8bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1250 &i_offset );
1251 break;
1253 case 0x20:
1254 case 0x21:
1255 case 0x22:
1256 /* We don't use map tables */
1257 break;
1259 case 0xf0: /* End of line code */
1260 p_pixbuf += 2*p_region->i_width;
1261 i_offset = 0; i_y += 2;
1262 break;
1267 static void dvbsub_pdata2bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1269 bool b_stop = false;
1271 while( !b_stop && !bs_eof( s ) )
1273 int i_count = 0, i_color = 0;
1275 i_color = bs_read( s, 2 );
1276 if( i_color != 0x00 )
1278 i_count = 1;
1280 else
1282 if( bs_read( s, 1 ) == 0x01 ) // Switch1
1284 i_count = 3 + bs_read( s, 3 );
1285 i_color = bs_read( s, 2 );
1287 else
1289 if( bs_read( s, 1 ) == 0x00 ) //Switch2
1291 switch( bs_read( s, 2 ) ) //Switch3
1293 case 0x00:
1294 b_stop = true;
1295 break;
1296 case 0x01:
1297 i_count = 2;
1298 break;
1299 case 0x02:
1300 i_count = 12 + bs_read( s, 4 );
1301 i_color = bs_read( s, 2 );
1302 break;
1303 case 0x03:
1304 i_count = 29 + bs_read( s, 8 );
1305 i_color = bs_read( s, 2 );
1306 break;
1307 default:
1308 break;
1311 else
1313 /* 1 pixel color 0 */
1314 i_count = 1;
1319 if( !i_count ) continue;
1321 /* Sanity check */
1322 if( ( i_count + *pi_off ) > i_width ) break;
1324 if( i_count == 1 ) p[*pi_off] = i_color;
1325 else memset( ( p + *pi_off ), i_color, i_count );
1327 (*pi_off) += i_count;
1330 bs_align( s );
1333 static void dvbsub_pdata4bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1335 bool b_stop = false;
1337 while( !b_stop && !bs_eof( s ) )
1339 int i_count = 0, i_color = 0;
1341 i_color = bs_read( s, 4 );
1342 if( i_color != 0x00 )
1344 /* Add 1 pixel */
1345 i_count = 1;
1347 else
1349 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1351 if( bs_show( s, 3 ) != 0x00 )
1353 i_count = 2 + bs_read( s, 3 );
1355 else
1357 bs_skip( s, 3 );
1358 b_stop = true;
1361 else
1363 if( bs_read( s, 1 ) == 0x00) //Switch2
1365 i_count = 4 + bs_read( s, 2 );
1366 i_color = bs_read( s, 4 );
1368 else
1370 switch ( bs_read( s, 2 ) ) //Switch3
1372 case 0x0:
1373 i_count = 1;
1374 break;
1375 case 0x1:
1376 i_count = 2;
1377 break;
1378 case 0x2:
1379 i_count = 9 + bs_read( s, 4 );
1380 i_color = bs_read( s, 4 );
1381 break;
1382 case 0x3:
1383 i_count= 25 + bs_read( s, 8 );
1384 i_color = bs_read( s, 4 );
1385 break;
1391 if( !i_count ) continue;
1393 /* Sanity check */
1394 if( ( i_count + *pi_off ) > i_width ) break;
1396 if( i_count == 1 ) p[*pi_off] = i_color;
1397 else memset( ( p + *pi_off ), i_color, i_count );
1399 (*pi_off) += i_count;
1402 bs_align( s );
1405 static void dvbsub_pdata8bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1407 bool b_stop = false;
1409 while( !b_stop && !bs_eof( s ) )
1411 int i_count = 0, i_color = 0;
1413 i_color = bs_read( s, 8 );
1414 if( i_color != 0x00 )
1416 /* Add 1 pixel */
1417 i_count = 1;
1419 else
1421 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1423 if( bs_show( s, 7 ) != 0x00 )
1425 i_count = bs_read( s, 7 );
1427 else
1429 bs_skip( s, 7 );
1430 b_stop = true;
1433 else
1435 i_count = bs_read( s, 7 );
1436 i_color = bs_read( s, 8 );
1440 if( !i_count ) continue;
1442 /* Sanity check */
1443 if( ( i_count + *pi_off ) > i_width ) break;
1445 if( i_count == 1 ) p[*pi_off] = i_color;
1446 else memset( ( p + *pi_off ), i_color, i_count );
1448 (*pi_off) += i_count;
1451 bs_align( s );
1454 static void free_all( decoder_t *p_dec )
1456 decoder_sys_t *p_sys = p_dec->p_sys;
1457 dvbsub_region_t *p_reg, *p_reg_next;
1458 dvbsub_clut_t *p_clut, *p_clut_next;
1460 /*free( p_sys->p_display ); No longer malloced */
1462 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut_next )
1464 p_clut_next = p_clut->p_next;
1465 free( p_clut );
1467 p_sys->p_cluts = NULL;
1469 for( p_reg = p_sys->p_regions; p_reg != NULL; p_reg = p_reg_next )
1471 p_reg_next = p_reg->p_next;
1472 for( int i = 0; i < p_reg->i_object_defs; i++ )
1473 free( p_reg->p_object_defs[i].psz_text );
1474 if( p_reg->i_object_defs ) free( p_reg->p_object_defs );
1475 free( p_reg->p_pixbuf );
1476 free( p_reg );
1478 p_sys->p_regions = NULL;
1480 if( p_sys->p_page )
1482 if( p_sys->p_page->i_region_defs )
1483 free( p_sys->p_page->p_region_defs );
1484 free( p_sys->p_page );
1486 p_sys->p_page = NULL;
1489 static subpicture_t *render( decoder_t *p_dec )
1491 decoder_sys_t *p_sys = p_dec->p_sys;
1492 subpicture_t *p_spu;
1493 subpicture_region_t **pp_spu_region;
1494 int i, j;
1495 int i_base_x;
1496 int i_base_y;
1498 /* Allocate the subpicture internal data. */
1499 p_spu = decoder_NewSubpicture( p_dec, NULL );
1500 if( !p_spu )
1501 return NULL;
1503 p_spu->b_absolute = p_sys->b_absolute;
1504 /* Set the pf_render callback */
1505 p_spu->i_start = p_sys->i_pts;
1506 //p_spu->i_stop = (mtime_t) 0;
1507 p_spu->b_ephemer = true;
1508 //p_spu->b_fade = true;
1509 //p_spu->i_stop = p_spu->i_start + (mtime_t) (i_timeout * 1000000);
1510 p_spu->b_subtitle = true;
1512 /* Correct positioning of SPU */
1513 i_base_x = p_sys->i_spu_x;
1514 i_base_y = p_sys->i_spu_y;
1516 p_spu->i_original_picture_width = p_sys->display.i_width;
1517 p_spu->i_original_picture_height = p_sys->display.i_height;
1519 if( p_sys->display.b_windowed )
1521 /* From en_300743v01 - */
1522 /* the DDS is there to indicate intended size/position of text */
1523 /* the intended video area is ->i_width/height */
1524 /* the window is within this... SO... we should leave i_original_picture_width etc. as is */
1525 /* and ONLY change i_base_x. effectively i_max_x/y are only there to limit memory requirements*/
1526 /* we COULD use the upper limits to limit rendering to within these? */
1528 /* see notes on DDS at the top of the file */
1529 i_base_x += p_sys->display.i_x;
1530 i_base_y += p_sys->display.i_y;
1533 pp_spu_region = &p_spu->p_region;
1535 /* Loop on region definitions */
1536 #ifdef DEBUG_DVBSUB
1537 if( p_sys->p_page )
1538 msg_Dbg( p_dec, "rendering %i regions", p_sys->p_page->i_region_defs );
1539 #endif
1541 for( i = 0; p_sys->p_page && ( i < p_sys->p_page->i_region_defs ); i++ )
1543 dvbsub_region_t *p_region;
1544 dvbsub_regiondef_t *p_regiondef;
1545 dvbsub_clut_t *p_clut;
1546 dvbsub_color_t *p_color;
1547 subpicture_region_t *p_spu_region;
1548 uint8_t *p_src, *p_dst;
1549 video_format_t fmt;
1550 video_palette_t palette;
1551 int i_pitch;
1553 p_regiondef = &p_sys->p_page->p_region_defs[i];
1555 /* Find associated region */
1556 for( p_region = p_sys->p_regions; p_region != NULL;
1557 p_region = p_region->p_next )
1559 if( p_regiondef->i_id == p_region->i_id ) break;
1562 #ifdef DEBUG_DVBSUB
1563 /* if a region exists, then print it's size */
1564 if (p_region)
1566 msg_Dbg( p_dec, "rendering region %i (%i,%i) to (%i,%i)", i,
1567 p_regiondef->i_x, p_regiondef->i_y,
1568 p_regiondef->i_x + p_region->i_width,
1569 p_regiondef->i_y + p_region->i_height );
1571 else
1573 msg_Dbg( p_dec, "rendering region %i (%i,%i) (no region matched to render)", i,
1574 p_regiondef->i_x, p_regiondef->i_y );
1576 #endif
1578 if( !p_region )
1580 msg_Dbg( p_dec, "region %i not found", p_regiondef->i_id );
1581 continue;
1584 /* Find associated CLUT */
1585 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next )
1587 if( p_region->i_clut == p_clut->i_id ) break;
1589 if( !p_clut )
1591 msg_Dbg( p_dec, "clut %i not found", p_region->i_clut );
1592 continue;
1595 /* FIXME: don't create a subpicture region with VLC CODEC YUVP
1596 * when it actually is a TEXT region */
1598 /* Create new SPU region */
1599 video_format_Init( &fmt, VLC_CODEC_YUVP );
1600 fmt.i_sar_num = 0; /* 0 means use aspect ratio of background video */
1601 fmt.i_sar_den = 1;
1602 fmt.i_width = fmt.i_visible_width = p_region->i_width;
1603 fmt.i_height = fmt.i_visible_height = p_region->i_height;
1604 fmt.i_x_offset = fmt.i_y_offset = 0;
1605 fmt.p_palette = &palette;
1606 fmt.p_palette->i_entries = ( p_region->i_depth == 1 ) ? 4 :
1607 ( ( p_region->i_depth == 2 ) ? 16 : 256 );
1608 p_color = ( p_region->i_depth == 1 ) ? p_clut->c_2b :
1609 ( ( p_region->i_depth == 2 ) ? p_clut->c_4b : p_clut->c_8b );
1610 for( j = 0; j < fmt.p_palette->i_entries; j++ )
1612 fmt.p_palette->palette[j][0] = p_color[j].Y;
1613 fmt.p_palette->palette[j][1] = p_color[j].Cb; /* U == Cb */
1614 fmt.p_palette->palette[j][2] = p_color[j].Cr; /* V == Cr */
1615 fmt.p_palette->palette[j][3] = 0xff - p_color[j].T;
1618 p_spu_region = subpicture_region_New( &fmt );
1619 fmt.p_palette = NULL; /* was stack var */
1620 video_format_Clean( &fmt );
1621 if( !p_spu_region )
1623 msg_Err( p_dec, "cannot allocate SPU region" );
1624 continue;
1626 p_spu_region->i_x = i_base_x + p_regiondef->i_x;
1627 p_spu_region->i_y = i_base_y + p_regiondef->i_y;
1628 p_spu_region->i_align = p_sys->i_spu_position;
1629 *pp_spu_region = p_spu_region;
1630 pp_spu_region = &p_spu_region->p_next;
1632 p_src = p_region->p_pixbuf;
1633 p_dst = p_spu_region->p_picture->Y_PIXELS;
1634 i_pitch = p_spu_region->p_picture->Y_PITCH;
1636 /* Copy pixel buffer */
1637 for( j = 0; j < p_region->i_height; j++ )
1639 memcpy( p_dst, p_src, p_region->i_width );
1640 p_src += p_region->i_width;
1641 p_dst += i_pitch;
1644 /* Check subtitles encoded as strings of characters
1645 * (since there are not rendered in the pixbuffer) */
1646 for( j = 0; j < p_region->i_object_defs; j++ )
1648 dvbsub_objectdef_t *p_object_def = &p_region->p_object_defs[j];
1650 if( ( p_object_def->i_type != 1 ) || !p_object_def->psz_text )
1651 continue;
1653 /* Create new SPU region */
1654 video_format_Init( &fmt, VLC_CODEC_TEXT );
1655 fmt.i_sar_num = 1;
1656 fmt.i_sar_den = 1;
1657 fmt.i_width = fmt.i_visible_width = p_region->i_width;
1658 fmt.i_height = fmt.i_visible_height = p_region->i_height;
1659 fmt.i_x_offset = fmt.i_y_offset = 0;
1660 p_spu_region = subpicture_region_New( &fmt );
1661 video_format_Clean( &fmt );
1663 p_spu_region->p_text = text_segment_New( p_object_def->psz_text );
1664 p_spu_region->i_x = i_base_x + p_regiondef->i_x + p_object_def->i_x;
1665 p_spu_region->i_y = i_base_y + p_regiondef->i_y + p_object_def->i_y;
1666 p_spu_region->i_align = p_sys->i_spu_position;
1667 *pp_spu_region = p_spu_region;
1668 pp_spu_region = &p_spu_region->p_next;
1672 return p_spu;
1675 /*****************************************************************************
1676 * encoder_sys_t : encoder descriptor
1677 *****************************************************************************/
1678 typedef struct encoder_region_t
1680 int i_width;
1681 int i_height;
1683 } encoder_region_t;
1685 struct encoder_sys_t
1687 unsigned int i_page_ver;
1688 unsigned int i_region_ver;
1689 unsigned int i_clut_ver;
1691 int i_regions;
1692 encoder_region_t *p_regions;
1694 mtime_t i_pts;
1696 /* subpicture positioning */
1697 int i_offset_x;
1698 int i_offset_y;
1701 #ifdef ENABLE_SOUT
1702 static void encode_page_composition( encoder_t *, bs_t *, subpicture_t * );
1703 static void encode_clut( encoder_t *, bs_t *, subpicture_t * );
1704 static void encode_region_composition( encoder_t *, bs_t *, subpicture_t * );
1705 static void encode_object( encoder_t *, bs_t *, subpicture_t * );
1707 /*****************************************************************************
1708 * OpenEncoder: probe the encoder and return score
1709 *****************************************************************************/
1710 static int OpenEncoder( vlc_object_t *p_this )
1712 encoder_t *p_enc = (encoder_t *)p_this;
1713 encoder_sys_t *p_sys;
1715 if( ( p_enc->fmt_out.i_codec != VLC_CODEC_DVBS ) &&
1716 !p_enc->obj.force )
1718 return VLC_EGENERIC;
1721 /* Allocate the memory needed to store the decoder's structure */
1722 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1723 return VLC_ENOMEM;
1724 p_enc->p_sys = p_sys;
1726 p_enc->pf_encode_sub = Encode;
1727 p_enc->fmt_out.i_codec = VLC_CODEC_DVBS;
1728 p_enc->fmt_out.subs.dvb.i_id = 1 << 16 | 1;
1730 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
1732 p_sys->i_page_ver = 0;
1733 p_sys->i_region_ver = 0;
1734 p_sys->i_clut_ver = 0;
1735 p_sys->i_regions = 0;
1736 p_sys->p_regions = 0;
1738 p_sys->i_offset_x = var_CreateGetInteger( p_this, ENC_CFG_PREFIX "x" );
1739 p_sys->i_offset_y = var_CreateGetInteger( p_this, ENC_CFG_PREFIX "y" );
1741 return VLC_SUCCESS;
1744 /* FIXME: this routine is a hack to convert VLC_CODEC_YUVA
1745 * into VLC_CODEC_YUVP
1747 static subpicture_t *YuvaYuvp( subpicture_t *p_subpic )
1749 subpicture_region_t *p_region = NULL;
1751 if( !p_subpic ) return NULL;
1753 for( p_region = p_subpic->p_region; p_region; p_region = p_region->p_next )
1755 video_format_t *p_fmt = &p_region->fmt;
1756 int i = 0, j = 0, n = 0, p = 0;
1757 int i_max_entries = 256;
1759 #ifdef RANDOM_DITHERING
1760 int i_seed = 0xdeadbeef; /* random seed */
1761 #else
1762 int *pi_delta;
1763 #endif
1764 int i_pixels = p_region->p_picture->p[0].i_visible_lines
1765 * p_region->p_picture->p[0].i_pitch;
1766 int i_iterator = p_region->p_picture->p[0].i_visible_lines * 3 / 4
1767 * p_region->p_picture->p[0].i_pitch
1768 + p_region->p_picture->p[0].i_pitch * 1 / 3;
1769 int i_tolerance = 0;
1771 #ifdef DEBUG_DVBSUB1
1772 /* p_enc not valid here */
1773 msg_Dbg( p_enc, "YuvaYuvp: i_pixels=%d, i_iterator=%d", i_pixels, i_iterator );
1774 #endif
1775 p_fmt->i_chroma = VLC_CODEC_YUVP;
1776 p_fmt->p_palette = (video_palette_t *) malloc( sizeof( video_palette_t ) );
1777 if( !p_fmt->p_palette ) break;
1778 p_fmt->p_palette->i_entries = 0;
1780 /* Find best iterator using Euclide’s algorithm */
1781 for( ; i_iterator > 1 ; i_iterator-- )
1783 int a = i_pixels;
1784 int b = i_iterator;
1785 int c;
1787 while( b )
1789 c = a % b;
1790 a = b;
1791 b = c;
1794 if( a == 1 )
1796 break;
1800 /* Count colors, build best palette */
1801 for( i_tolerance = 0; i_tolerance < 128; i_tolerance++ )
1803 bool b_success = true;
1804 p_fmt->p_palette->i_entries = 0;
1806 for( i = 0; i < i_pixels ; )
1808 uint8_t y, u, v, a;
1809 y = p_region->p_picture->p[0].p_pixels[i];
1810 u = p_region->p_picture->p[1].p_pixels[i];
1811 v = p_region->p_picture->p[2].p_pixels[i];
1812 a = p_region->p_picture->p[3].p_pixels[i];
1813 for( j = 0; j < p_fmt->p_palette->i_entries; j++ )
1815 if( abs((int)p_fmt->p_palette->palette[j][0] - (int)y) <= i_tolerance &&
1816 abs((int)p_fmt->p_palette->palette[j][1] - (int)u) <= i_tolerance &&
1817 abs((int)p_fmt->p_palette->palette[j][2] - (int)v) <= i_tolerance &&
1818 abs((int)p_fmt->p_palette->palette[j][3] - (int)a) <= i_tolerance / 2 )
1820 break;
1823 if( j == p_fmt->p_palette->i_entries )
1825 p_fmt->p_palette->palette[j][0] = y;
1826 p_fmt->p_palette->palette[j][1] = u;
1827 p_fmt->p_palette->palette[j][2] = v;
1828 p_fmt->p_palette->palette[j][3] = a;
1829 p_fmt->p_palette->i_entries++;
1831 if( p_fmt->p_palette->i_entries >= i_max_entries )
1833 b_success = false;
1834 break;
1836 i += i_iterator;
1837 if( i > i_pixels )
1839 i -= i_pixels;
1843 if( b_success )
1845 break;
1849 #ifdef DEBUG_DVBSUB1
1850 /* p_enc not valid here */
1851 msg_Dbg( p_enc, "best palette has %d colors", p_fmt->p_palette->i_entries );
1852 #endif
1854 #ifndef RANDOM_DITHERING
1855 pi_delta = xmalloc( ( p_region->p_picture->p[0].i_pitch + 1 )
1856 * sizeof(int) * 4 );
1857 for( i = 0; i < (p_region->p_picture->p[0].i_pitch + 1) * 4 ; i++ )
1859 pi_delta[ i ] = 0;
1861 #endif
1863 /* Fill image with our new colours */
1864 for( p = 0; p < p_region->p_picture->p[0].i_visible_lines ; p++ )
1866 int i_ydelta = 0, i_udelta = 0, i_vdelta = 0, i_adelta = 0;
1868 for( n = 0; n < p_region->p_picture->p[0].i_pitch ; n++ )
1870 int i_offset = p * p_region->p_picture->p[0].i_pitch + n;
1871 int y, u, v, a;
1872 int i_mindist, i_best;
1874 y = (int)p_region->p_picture->p[0].p_pixels[i_offset];
1875 u = (int)p_region->p_picture->p[1].p_pixels[i_offset];
1876 v = (int)p_region->p_picture->p[2].p_pixels[i_offset];
1877 a = (int)p_region->p_picture->p[3].p_pixels[i_offset];
1879 /* Add dithering compensation */
1880 #ifdef RANDOM_DITHERING
1881 y += ((i_seed & 0xff) - 0x80) * i_tolerance / 0x80;
1882 u += (((i_seed >> 8) & 0xff) - 0x80) * i_tolerance / 0x80;
1883 v += (((i_seed >> 16) & 0xff) - 0x80) * i_tolerance / 0x80;
1884 a += (((i_seed >> 24) & 0xff) - 0x80) * i_tolerance / 0x80;
1885 #else
1886 y += i_ydelta + pi_delta[ n * 4 ];
1887 u += i_udelta + pi_delta[ n * 4 + 1 ];
1888 v += i_vdelta + pi_delta[ n * 4 + 2 ];
1889 a += i_adelta + pi_delta[ n * 4 + 3 ];
1890 #endif
1892 /* Find best colour in palette */
1893 for( i_mindist = 99999999, i_best = 0, j = 0; j < p_fmt->p_palette->i_entries; j++ )
1895 int i_dist = 0;
1897 i_dist += abs((int)p_fmt->p_palette->palette[j][0] - y);
1898 i_dist += abs((int)p_fmt->p_palette->palette[j][1] - u);
1899 i_dist += abs((int)p_fmt->p_palette->palette[j][2] - v);
1900 i_dist += 2 * abs((int)p_fmt->p_palette->palette[j][3] - a);
1902 if( i_dist < i_mindist )
1904 i_mindist = i_dist;
1905 i_best = j;
1909 /* Set pixel to best color */
1910 p_region->p_picture->p[0].p_pixels[i_offset] = i_best;
1912 /* Update dithering state */
1913 #ifdef RANDOM_DITHERING
1914 i_seed = (i_seed * 0x1283837) ^ 0x789479 ^ (i_seed >> 13);
1915 #else
1916 i_ydelta = y - (int)p_fmt->p_palette->palette[i_best][0];
1917 i_udelta = u - (int)p_fmt->p_palette->palette[i_best][1];
1918 i_vdelta = v - (int)p_fmt->p_palette->palette[i_best][2];
1919 i_adelta = a - (int)p_fmt->p_palette->palette[i_best][3];
1920 pi_delta[ n * 4 ] = i_ydelta * 3 / 8;
1921 pi_delta[ n * 4 + 1 ] = i_udelta * 3 / 8;
1922 pi_delta[ n * 4 + 2 ] = i_vdelta * 3 / 8;
1923 pi_delta[ n * 4 + 3 ] = i_adelta * 3 / 8;
1924 i_ydelta = i_ydelta * 5 / 8;
1925 i_udelta = i_udelta * 5 / 8;
1926 i_vdelta = i_vdelta * 5 / 8;
1927 i_adelta = i_adelta * 5 / 8;
1928 #endif
1931 #ifndef RANDOM_DITHERING
1932 free( pi_delta );
1933 #endif
1935 /* pad palette */
1936 for( i = p_fmt->p_palette->i_entries; i < i_max_entries; i++ )
1938 p_fmt->p_palette->palette[i][0] = 0;
1939 p_fmt->p_palette->palette[i][1] = 0;
1940 p_fmt->p_palette->palette[i][2] = 0;
1941 p_fmt->p_palette->palette[i][3] = 0;
1943 p_fmt->p_palette->i_entries = i_max_entries;
1944 #ifdef DEBUG_DVBSUB1
1945 /* p_enc not valid here */
1946 msg_Dbg( p_enc, "best palette has %d colors", p_fmt->p_palette->i_entries );
1947 #endif
1949 return p_subpic;
1950 } /* End of hack */
1952 /****************************************************************************
1953 * Encode: the whole thing
1954 ****************************************************************************/
1955 static block_t *Encode( encoder_t *p_enc, subpicture_t *p_subpic )
1957 subpicture_t *p_temp = NULL;
1958 subpicture_region_t *p_region = NULL;
1959 bs_t bits, *s = &bits;
1960 block_t *p_block;
1962 if( !p_subpic || !p_subpic->p_region ) return NULL;
1964 /* FIXME: this is a hack to convert VLC_CODEC_YUVA into
1965 * VLC_CODEC_YUVP
1967 p_region = p_subpic->p_region;
1968 if( p_region->fmt.i_chroma == VLC_CODEC_YUVA )
1970 p_temp = YuvaYuvp( p_subpic );
1971 if( !p_temp )
1973 msg_Err( p_enc, "no picture in subpicture" );
1974 return NULL;
1976 p_region = p_subpic->p_region;
1979 /* Sanity check */
1980 if( !p_region ) return NULL;
1982 if( ( p_region->fmt.i_chroma != VLC_CODEC_TEXT ) &&
1983 ( p_region->fmt.i_chroma != VLC_CODEC_YUVP ) )
1985 msg_Err( p_enc, "chroma %4.4s not supported", (char *)&p_region->fmt.i_chroma );
1986 return NULL;
1989 if( p_region->fmt.p_palette )
1991 switch( p_region->fmt.p_palette->i_entries )
1993 case 0:
1994 case 4:
1995 case 16:
1996 case 256:
1997 break;
1998 default:
1999 msg_Err( p_enc, "subpicture palette (%d) not handled",
2000 p_region->fmt.p_palette->i_entries );
2001 return NULL;
2004 /* End of hack */
2006 #ifdef DEBUG_DVBSUB
2007 msg_Dbg( p_enc, "encoding subpicture" );
2008 #endif
2009 p_block = block_Alloc( 64000 );
2010 bs_init( s, p_block->p_buffer, p_block->i_buffer );
2012 bs_write( s, 8, 0x20 ); /* Data identifier */
2013 bs_write( s, 8, 0x0 ); /* Subtitle stream id */
2015 encode_page_composition( p_enc, s, p_subpic );
2016 encode_region_composition( p_enc, s, p_subpic );
2017 encode_clut( p_enc, s, p_subpic );
2018 encode_object( p_enc, s, p_subpic );
2020 /* End of display */
2021 bs_write( s, 8, 0x0f ); /* Sync byte */
2022 bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
2023 bs_write( s, 16, 1 ); /* Page id */
2024 bs_write( s, 16, 0 ); /* Segment length */
2026 bs_write( s, 8, 0xff );/* End marker */
2027 p_block->i_buffer = bs_pos( s ) / 8;
2028 p_block->i_pts = p_block->i_dts = p_subpic->i_start;
2029 if( !p_subpic->b_ephemer && ( p_subpic->i_stop > p_subpic->i_start ) )
2031 block_t *p_block_stop;
2033 p_block->i_length = p_subpic->i_stop - p_subpic->i_start;
2035 /* Send another (empty) subtitle to signal the end of display */
2036 p_block_stop = block_Alloc( 64000 );
2037 bs_init( s, p_block_stop->p_buffer, p_block_stop->i_buffer );
2038 bs_write( s, 8, 0x20 ); /* Data identifier */
2039 bs_write( s, 8, 0x0 ); /* Subtitle stream id */
2040 encode_page_composition( p_enc, s, 0 );
2041 bs_write( s, 8, 0x0f ); /* Sync byte */
2042 bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
2043 bs_write( s, 16, 1 ); /* Page id */
2044 bs_write( s, 16, 0 ); /* Segment length */
2045 bs_write( s, 8, 0xff );/* End marker */
2046 p_block_stop->i_buffer = bs_pos( s ) / 8;
2047 p_block_stop->i_pts = p_block_stop->i_dts = p_subpic->i_stop;
2048 block_ChainAppend( &p_block, p_block_stop );
2049 p_block_stop->i_length = 100000; /* p_subpic->i_stop - p_subpic->i_start; */
2051 #ifdef DEBUG_DVBSUB
2052 msg_Dbg( p_enc, "subpicture encoded properly" );
2053 #endif
2054 return p_block;
2057 /*****************************************************************************
2058 * CloseEncoder: encoder destruction
2059 *****************************************************************************/
2060 static void CloseEncoder( vlc_object_t *p_this )
2062 encoder_t *p_enc = (encoder_t *)p_this;
2063 encoder_sys_t *p_sys = p_enc->p_sys;
2065 var_Destroy( p_this , ENC_CFG_PREFIX "x" );
2066 var_Destroy( p_this , ENC_CFG_PREFIX "y" );
2067 var_Destroy( p_this , ENC_CFG_PREFIX "timeout" );
2069 if( p_sys->i_regions ) free( p_sys->p_regions );
2070 free( p_sys );
2073 static void encode_page_composition( encoder_t *p_enc, bs_t *s,
2074 subpicture_t *p_subpic )
2076 encoder_sys_t *p_sys = p_enc->p_sys;
2077 subpicture_region_t *p_region;
2078 bool b_mode_change = false;
2079 int i_regions, i_timeout;
2081 bs_write( s, 8, 0x0f ); /* Sync byte */
2082 bs_write( s, 8, DVBSUB_ST_PAGE_COMPOSITION ); /* Segment type */
2083 bs_write( s, 16, 1 ); /* Page id */
2085 for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
2086 p_region; p_region = p_region->p_next, i_regions++ )
2088 if( i_regions >= p_sys->i_regions )
2090 encoder_region_t region;
2091 region.i_width = region.i_height = 0;
2092 p_sys->p_regions = xrealloc( p_sys->p_regions,
2093 sizeof(encoder_region_t) * (p_sys->i_regions + 1) );
2094 p_sys->p_regions[p_sys->i_regions++] = region;
2097 if( ( p_sys->p_regions[i_regions].i_width <
2098 (int)p_region->fmt.i_visible_width ) ||
2099 ( p_sys->p_regions[i_regions].i_width >
2100 (int)p_region->fmt.i_visible_width ) )
2102 b_mode_change = true;
2103 msg_Dbg( p_enc, "region %i width change: %i -> %i",
2104 i_regions, p_sys->p_regions[i_regions].i_width,
2105 p_region->fmt.i_visible_width );
2106 p_sys->p_regions[i_regions].i_width =
2107 p_region->fmt.i_visible_width;
2109 if( p_sys->p_regions[i_regions].i_height <
2110 (int)p_region->fmt.i_visible_height )
2112 b_mode_change = true;
2113 msg_Dbg( p_enc, "region %i height change: %i -> %i",
2114 i_regions, p_sys->p_regions[i_regions].i_height,
2115 p_region->fmt.i_visible_height );
2116 p_sys->p_regions[i_regions].i_height =
2117 p_region->fmt.i_visible_height;
2121 bs_write( s, 16, i_regions * 6 + 2 ); /* Segment length */
2123 i_timeout = 0;
2124 if( p_subpic && !p_subpic->b_ephemer &&
2125 ( p_subpic->i_stop > p_subpic->i_start ) )
2127 i_timeout = (p_subpic->i_stop - p_subpic->i_start) / 1000000;
2130 bs_write( s, 8, i_timeout ); /* Timeout */
2131 bs_write( s, 4, p_sys->i_page_ver++ );
2132 bs_write( s, 2, b_mode_change ?
2133 DVBSUB_PCS_STATE_CHANGE : DVBSUB_PCS_STATE_ACQUISITION );
2134 bs_write( s, 2, 0 ); /* Reserved */
2136 for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
2137 p_region; p_region = p_region->p_next, i_regions++ )
2139 bs_write( s, 8, i_regions );
2140 bs_write( s, 8, 0 ); /* Reserved */
2141 if( (p_sys->i_offset_x > 0) && (p_sys->i_offset_y > 0) )
2143 bs_write( s, 16, p_sys->i_offset_x ); /* override x position */
2144 bs_write( s, 16, p_sys->i_offset_y ); /* override y position */
2146 else
2148 bs_write( s, 16, p_region->i_x );
2149 bs_write( s, 16, p_region->i_y );
2154 static void encode_clut( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
2156 encoder_sys_t *p_sys = p_enc->p_sys;
2157 subpicture_region_t *p_region = p_subpic->p_region;
2158 video_palette_t *p_pal, pal;
2160 /* Sanity check */
2161 if( !p_region ) return;
2163 if( p_region->fmt.i_chroma == VLC_CODEC_YUVP )
2165 p_pal = p_region->fmt.p_palette;
2167 else
2169 pal.i_entries = 4;
2170 for( int i = 0; i < 4; i++ )
2172 pal.palette[i][0] = 0;
2173 pal.palette[i][1] = 0;
2174 pal.palette[i][2] = 0;
2175 pal.palette[i][3] = 0;
2177 p_pal = &pal;
2180 bs_write( s, 8, 0x0f ); /* Sync byte */
2181 bs_write( s, 8, DVBSUB_ST_CLUT_DEFINITION ); /* Segment type */
2182 bs_write( s, 16, 1 ); /* Page id */
2184 bs_write( s, 16, p_pal->i_entries * 6 + 2 ); /* Segment length */
2185 bs_write( s, 8, 1 ); /* Clut id */
2186 bs_write( s, 4, p_sys->i_clut_ver++ );
2187 bs_write( s, 4, 0 ); /* Reserved */
2189 for( int i = 0; i < p_pal->i_entries; i++ )
2191 bs_write( s, 8, i ); /* Clut entry id */
2192 bs_write( s, 1, p_pal->i_entries == 4 ); /* 2bit/entry flag */
2193 bs_write( s, 1, p_pal->i_entries == 16 ); /* 4bit/entry flag */
2194 bs_write( s, 1, p_pal->i_entries == 256 ); /* 8bit/entry flag */
2195 bs_write( s, 4, 0 ); /* Reserved */
2196 bs_write( s, 1, 1 ); /* Full range flag */
2197 bs_write( s, 8, p_pal->palette[i][3] ? /* Y value */
2198 (p_pal->palette[i][0] ? p_pal->palette[i][0] : 16) : 0 );
2199 bs_write( s, 8, p_pal->palette[i][1] ); /* Cr value */
2200 bs_write( s, 8, p_pal->palette[i][2] ); /* Cb value */
2201 bs_write( s, 8, 0xff - p_pal->palette[i][3] ); /* T value */
2205 static void encode_region_composition( encoder_t *p_enc, bs_t *s,
2206 subpicture_t *p_subpic )
2208 encoder_sys_t *p_sys = p_enc->p_sys;
2209 subpicture_region_t *p_region;
2210 int i_region;
2212 for( i_region = 0, p_region = p_subpic->p_region; p_region;
2213 p_region = p_region->p_next, i_region++ )
2215 int i_entries = 4, i_depth = 0x1, i_bg = 0;
2216 bool b_text =
2217 ( p_region->fmt.i_chroma == VLC_CODEC_TEXT );
2219 if( !b_text )
2221 video_palette_t *p_pal = p_region->fmt.p_palette;
2223 if( !p_pal )
2225 msg_Err( p_enc, "subpicture has no palette - ignoring it" );
2226 break;
2229 i_entries = p_pal->i_entries;
2230 i_depth = i_entries == 4 ? 0x1 : i_entries == 16 ? 0x2 : 0x3;
2232 for( i_bg = 0; i_bg < p_pal->i_entries; i_bg++ )
2234 if( !p_pal->palette[i_bg][3] ) break;
2238 bs_write( s, 8, 0x0f ); /* Sync byte */
2239 bs_write( s, 8, DVBSUB_ST_REGION_COMPOSITION ); /* Segment type */
2240 bs_write( s, 16, 1 ); /* Page id */
2242 bs_write( s, 16, 10 + 6 + (b_text ? 2 : 0) ); /* Segment length */
2243 bs_write( s, 8, i_region );
2244 bs_write( s, 4, p_sys->i_region_ver++ );
2246 /* Region attributes */
2247 bs_write( s, 1, i_bg < i_entries ); /* Fill */
2248 bs_write( s, 3, 0 ); /* Reserved */
2249 bs_write( s, 16, p_sys->p_regions[i_region].i_width );
2250 bs_write( s, 16, p_sys->p_regions[i_region].i_height );
2251 bs_write( s, 3, i_depth ); /* Region level of compatibility */
2252 bs_write( s, 3, i_depth ); /* Region depth */
2253 bs_write( s, 2, 0 ); /* Reserved */
2254 bs_write( s, 8, 1 ); /* Clut id */
2255 bs_write( s, 8, i_bg ); /* region 8bit pixel code */
2256 bs_write( s, 4, i_bg ); /* region 4bit pixel code */
2257 bs_write( s, 2, i_bg ); /* region 2bit pixel code */
2258 bs_write( s, 2, 0 ); /* Reserved */
2260 /* In our implementation we only have 1 object per region */
2261 bs_write( s, 16, i_region );
2262 bs_write( s, 2, b_text ? DVBSUB_OT_BASIC_CHAR:DVBSUB_OT_BASIC_BITMAP );
2263 bs_write( s, 2, 0 ); /* object provider flag */
2264 bs_write( s, 12, 0 );/* object horizontal position */
2265 bs_write( s, 4, 0 ); /* Reserved */
2266 bs_write( s, 12, 0 );/* object vertical position */
2268 if( b_text )
2270 bs_write( s, 8, 1 ); /* foreground pixel code */
2271 bs_write( s, 8, 0 ); /* background pixel code */
2276 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
2277 subpicture_region_t *p_region,
2278 bool b_top );
2280 static void encode_object( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
2282 encoder_sys_t *p_sys = p_enc->p_sys;
2283 subpicture_region_t *p_region;
2284 int i_region;
2286 int i_length_pos, i_update_pos, i_pixel_data_pos;
2288 for( i_region = 0, p_region = p_subpic->p_region; p_region;
2289 p_region = p_region->p_next, i_region++ )
2291 bs_write( s, 8, 0x0f ); /* Sync byte */
2292 bs_write( s, 8, DVBSUB_ST_OBJECT_DATA ); /* Segment type */
2293 bs_write( s, 16, 1 ); /* Page id */
2295 i_length_pos = bs_pos( s );
2296 bs_write( s, 16, 0 ); /* Segment length */
2297 bs_write( s, 16, i_region ); /* Object id */
2298 bs_write( s, 4, p_sys->i_region_ver++ );
2300 /* object coding method */
2301 switch( p_region->fmt.i_chroma )
2303 case VLC_CODEC_YUVP:
2304 bs_write( s, 2, 0 );
2305 break;
2306 case VLC_CODEC_TEXT:
2307 bs_write( s, 2, 1 );
2308 break;
2309 default:
2310 msg_Err( p_enc, "FOURCC %d not supported by encoder.", p_region->fmt.i_chroma );
2311 continue;
2314 bs_write( s, 1, 0 ); /* non modifying color flag */
2315 bs_write( s, 1, 0 ); /* Reserved */
2317 if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
2319 int i_size, i;
2321 if( !p_region->p_text ) continue;
2323 i_size = __MIN( strlen( p_region->p_text->psz_text ), 256 );
2325 bs_write( s, 8, i_size ); /* number of characters in string */
2326 for( i = 0; i < i_size; i++ )
2328 bs_write( s, 16, p_region->p_text->psz_text[i] );
2331 /* Update segment length */
2332 SetWBE( &s->p_start[i_length_pos/8],
2333 (bs_pos(s) - i_length_pos)/8 -2 );
2334 continue;
2337 /* Coding of a bitmap object */
2338 i_update_pos = bs_pos( s );
2339 bs_write( s, 16, 0 ); /* topfield data block length */
2340 bs_write( s, 16, 0 ); /* bottomfield data block length */
2342 /* Top field */
2343 i_pixel_data_pos = bs_pos( s );
2344 encode_pixel_data( p_enc, s, p_region, true );
2345 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
2346 SetWBE( &s->p_start[i_update_pos/8], i_pixel_data_pos );
2348 /* Bottom field */
2349 i_pixel_data_pos = bs_pos( s );
2350 encode_pixel_data( p_enc, s, p_region, false );
2351 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
2352 SetWBE( &s->p_start[i_update_pos/8+2], i_pixel_data_pos );
2354 /* Stuffing for word alignment */
2355 bs_align_0( s );
2356 if( bs_pos( s ) % 16 ) bs_write( s, 8, 0 );
2358 /* Update segment length */
2359 SetWBE( &s->p_start[i_length_pos/8], (bs_pos(s) - i_length_pos)/8 -2 );
2363 static void encode_pixel_line_2bp( bs_t *s, subpicture_region_t *p_region,
2364 int i_line );
2365 static void encode_pixel_line_4bp( bs_t *s, subpicture_region_t *p_region,
2366 int i_line );
2367 static void encode_pixel_line_8bp( bs_t *s, subpicture_region_t *p_region,
2368 int i_line );
2369 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
2370 subpicture_region_t *p_region,
2371 bool b_top )
2373 unsigned int i_line;
2375 /* Sanity check */
2376 if( p_region->fmt.i_chroma != VLC_CODEC_YUVP ) return;
2378 /* Encode line by line */
2379 for( i_line = !b_top; i_line < p_region->fmt.i_visible_height;
2380 i_line += 2 )
2382 switch( p_region->fmt.p_palette->i_entries )
2384 case 0:
2385 break;
2387 case 4:
2388 bs_write( s, 8, 0x10 ); /* 2 bit/pixel code string */
2389 encode_pixel_line_2bp( s, p_region, i_line );
2390 break;
2392 case 16:
2393 bs_write( s, 8, 0x11 ); /* 4 bit/pixel code string */
2394 encode_pixel_line_4bp( s, p_region, i_line );
2395 break;
2397 case 256:
2398 bs_write( s, 8, 0x12 ); /* 8 bit/pixel code string */
2399 encode_pixel_line_8bp( s, p_region, i_line );
2400 break;
2402 default:
2403 msg_Err( p_enc, "subpicture palette (%i) not handled",
2404 p_region->fmt.p_palette->i_entries );
2405 break;
2408 bs_write( s, 8, 0xf0 ); /* End of object line code */
2412 static void encode_pixel_line_2bp( bs_t *s, subpicture_region_t *p_region,
2413 int i_line )
2415 unsigned int i, i_length = 0;
2416 int i_pitch = p_region->p_picture->p->i_pitch;
2417 uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
2418 int i_last_pixel = p_data[0];
2420 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2422 if( ( i != p_region->fmt.i_visible_width ) &&
2423 ( p_data[i] == i_last_pixel ) && ( i_length != 284 ) )
2425 i_length++;
2426 continue;
2429 if( ( i_length == 1 ) || ( i_length == 11 ) || ( i_length == 28 ) )
2431 /* 2bit/pixel code */
2432 if( i_last_pixel )
2433 bs_write( s, 2, i_last_pixel );
2434 else
2436 bs_write( s, 2, 0 );
2437 bs_write( s, 1, 0 );
2438 bs_write( s, 1, 1 ); /* pseudo color 0 */
2440 i_length--;
2443 if( i_length == 2 )
2445 if( i_last_pixel )
2447 bs_write( s, 2, i_last_pixel );
2448 bs_write( s, 2, i_last_pixel );
2450 else
2452 bs_write( s, 2, 0 );
2453 bs_write( s, 1, 0 );
2454 bs_write( s, 1, 0 );
2455 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
2458 else if( i_length > 2 )
2460 bs_write( s, 2, 0 );
2461 if( i_length <= 10 )
2463 bs_write( s, 1, 1 );
2464 bs_write( s, 3, i_length - 3 );
2465 bs_write( s, 2, i_last_pixel );
2467 else
2469 bs_write( s, 1, 0 );
2470 bs_write( s, 1, 0 );
2472 if( i_length <= 27 )
2474 bs_write( s, 2, 2 );
2475 bs_write( s, 4, i_length - 12 );
2476 bs_write( s, 2, i_last_pixel );
2478 else
2480 bs_write( s, 2, 3 );
2481 bs_write( s, 8, i_length - 29 );
2482 bs_write( s, 2, i_last_pixel );
2487 if( i == p_region->fmt.i_visible_width ) break;
2489 i_last_pixel = p_data[i];
2490 i_length = 1;
2493 /* Stop */
2494 bs_write( s, 2, 0 );
2495 bs_write( s, 1, 0 );
2496 bs_write( s, 1, 0 );
2497 bs_write( s, 2, 0 );
2499 /* Stuffing */
2500 bs_align_0( s );
2503 static void encode_pixel_line_4bp( bs_t *s, subpicture_region_t *p_region,
2504 int i_line )
2506 unsigned int i, i_length = 0;
2507 int i_pitch = p_region->p_picture->p->i_pitch;
2508 uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
2509 int i_last_pixel = p_data[0];
2511 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2513 if( i != p_region->fmt.i_visible_width &&
2514 p_data[i] == i_last_pixel && i_length != 280 )
2516 i_length++;
2517 continue;
2520 if( ( i_length == 1 ) ||
2521 ( ( i_length == 3 ) && i_last_pixel ) ||
2522 ( i_length == 8 ) )
2524 /* 4bit/pixel code */
2525 if( i_last_pixel )
2526 bs_write( s, 4, i_last_pixel );
2527 else
2529 bs_write( s, 4, 0 );
2530 bs_write( s, 1, 1 );
2531 bs_write( s, 1, 1 );
2532 bs_write( s, 2, 0 ); /* pseudo color 0 */
2534 i_length--;
2537 if( i_length == 2 )
2539 if( i_last_pixel )
2541 bs_write( s, 4, i_last_pixel );
2542 bs_write( s, 4, i_last_pixel );
2544 else
2546 bs_write( s, 4, 0 );
2547 bs_write( s, 1, 1 );
2548 bs_write( s, 1, 1 );
2549 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
2552 else if( !i_last_pixel && ( i_length >= 3 ) && ( i_length <= 9 ) )
2554 bs_write( s, 4, 0 );
2555 bs_write( s, 1, 0 );
2556 bs_write( s, 3, i_length - 2 ); /* (i_length - 2) * color 0 */
2558 else if( i_length > 2 )
2560 bs_write( s, 4, 0 );
2561 bs_write( s, 1, 1 );
2563 if( i_length <= 7 )
2565 bs_write( s, 1, 0 );
2566 bs_write( s, 2, i_length - 4 );
2567 bs_write( s, 4, i_last_pixel );
2569 else
2571 bs_write( s, 1, 1 );
2573 if( i_length <= 24 )
2575 bs_write( s, 2, 2 );
2576 bs_write( s, 4, i_length - 9 );
2577 bs_write( s, 4, i_last_pixel );
2579 else
2581 bs_write( s, 2, 3 );
2582 bs_write( s, 8, i_length - 25 );
2583 bs_write( s, 4, i_last_pixel );
2588 if( i == p_region->fmt.i_visible_width ) break;
2590 i_last_pixel = p_data[i];
2591 i_length = 1;
2594 /* Stop */
2595 bs_write( s, 8, 0 );
2597 /* Stuffing */
2598 bs_align_0( s );
2601 static void encode_pixel_line_8bp( bs_t *s, subpicture_region_t *p_region,
2602 int i_line )
2604 unsigned int i, i_length = 0;
2605 int i_pitch = p_region->p_picture->p->i_pitch;
2606 uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
2607 int i_last_pixel = p_data[0];
2609 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2611 if( ( i != p_region->fmt.i_visible_width ) &&
2612 ( p_data[i] == i_last_pixel ) && ( i_length != 127 ) )
2614 i_length++;
2615 continue;
2618 if( ( i_length == 1 ) && i_last_pixel )
2620 /* 8bit/pixel code */
2621 bs_write( s, 8, i_last_pixel );
2623 else if( ( i_length == 2 ) && i_last_pixel )
2625 /* 8bit/pixel code */
2626 bs_write( s, 8, i_last_pixel );
2627 bs_write( s, 8, i_last_pixel );
2629 else if( i_length <= 127 )
2631 bs_write( s, 8, 0 );
2633 if( !i_last_pixel )
2635 bs_write( s, 1, 0 );
2636 bs_write( s, 7, i_length ); /* pseudo color 0 */
2638 else
2640 bs_write( s, 1, 1 );
2641 bs_write( s, 7, i_length );
2642 bs_write( s, 8, i_last_pixel );
2646 if( i == p_region->fmt.i_visible_width ) break;
2648 i_last_pixel = p_data[i];
2649 i_length = 1;
2652 /* Stop */
2653 bs_write( s, 8, 0 );
2654 bs_write( s, 8, 0 );
2656 /* Stuffing */
2657 bs_align_0( s );
2660 #endif
2662 static void default_dds_init( decoder_t * p_dec )
2664 decoder_sys_t *p_sys = p_dec->p_sys;
2666 /* see notes on DDS at the top of the file */
2668 /* configure for SD res in case DDS is not present */
2669 p_sys->display.i_version = 0xff; /* an invalid version so it's always different */
2670 p_sys->display.i_width = 720;
2671 p_sys->display.i_height = 576;
2672 p_sys->display.b_windowed = false;