demux: heif: send extradata with avif
[vlc.git] / modules / codec / dvbsub.c
blob593436cd2534ed670cb6438925255e3ee6a1fc57
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 typedef struct
254 bs_t bs;
256 /* Decoder internal data */
257 int i_id;
258 int i_ancillary_id;
259 vlc_tick_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;
273 } decoder_sys_t;
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 *, uint16_t );
309 static void decode_region_composition( decoder_t *, bs_t *, uint16_t );
310 static void decode_object( decoder_t *, bs_t *, uint16_t );
311 static void decode_display_definition( decoder_t *, bs_t *, uint16_t );
312 static void decode_clut( decoder_t *, bs_t *, uint16_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_TICK_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_TICK_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_TICK_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;
462 uint8_t i_sync_byte = bs_read( &p_sys->bs, 8 );
463 while( i_sync_byte == 0x0f ) /* Sync byte */
465 decode_segment( p_dec, &p_sys->bs );
466 i_sync_byte = bs_read( &p_sys->bs, 8 );
469 if( ( i_sync_byte & 0x3f ) != 0x3f ) /* End marker */
471 msg_Warn( p_dec, "end marker not found (corrupted subtitle ?)" );
472 block_Release( p_block );
473 return VLCDEC_SUCCESS;
476 /* Check if the page is to be displayed */
477 if( p_sys->p_page && p_sys->b_page )
479 subpicture_t *p_spu = render( p_dec );
480 if( p_spu != NULL )
481 decoder_QueueSub( p_dec, p_spu );
484 block_Release( p_block );
486 return VLCDEC_SUCCESS;
489 /* following functions are local */
491 /*****************************************************************************
492 * default_clut_init: default clut as defined in EN 300-743 section 10
493 *****************************************************************************/
494 static void default_clut_init( decoder_t *p_dec )
496 decoder_sys_t *p_sys = p_dec->p_sys;
497 uint8_t i;
499 #define RGB_TO_Y(r, g, b) ((int16_t) 77 * r + 150 * g + 29 * b) / 256;
500 #define RGB_TO_U(r, g, b) ((int16_t) -44 * r - 87 * g + 131 * b) / 256;
501 #define RGB_TO_V(r, g, b) ((int16_t) 131 * r - 110 * g - 21 * b) / 256;
503 /* 4 entries CLUT */
504 for( i = 0; i < 4; i++ )
506 uint8_t R = 0, G = 0, B = 0, T = 0;
508 if( !(i & 0x2) && !(i & 0x1) ) T = 0xFF;
509 else if( !(i & 0x2) && (i & 0x1) ) R = G = B = 0xFF;
510 else if( (i & 0x2) && !(i & 0x1) ) R = G = B = 0;
511 else R = G = B = 0x7F;
513 p_sys->default_clut.c_2b[i].Y = RGB_TO_Y(R,G,B);
514 p_sys->default_clut.c_2b[i].Cb = RGB_TO_V(R,G,B);
515 p_sys->default_clut.c_2b[i].Cr = RGB_TO_U(R,G,B);
516 p_sys->default_clut.c_2b[i].T = T;
519 /* 16 entries CLUT */
520 for( i = 0; i < 16; i++ )
522 uint8_t R = 0, G = 0, B = 0, T = 0;
524 if( !(i & 0x8) )
526 if( !(i & 0x4) && !(i & 0x2) && !(i & 0x1) )
528 T = 0xFF;
530 else
532 R = (i & 0x1) ? 0xFF : 0;
533 G = (i & 0x2) ? 0xFF : 0;
534 B = (i & 0x4) ? 0xFF : 0;
537 else
539 R = (i & 0x1) ? 0x7F : 0;
540 G = (i & 0x2) ? 0x7F : 0;
541 B = (i & 0x4) ? 0x7F : 0;
544 p_sys->default_clut.c_4b[i].Y = RGB_TO_Y(R,G,B);
545 p_sys->default_clut.c_4b[i].Cr = RGB_TO_V(R,G,B);
546 p_sys->default_clut.c_4b[i].Cb = RGB_TO_U(R,G,B);
547 p_sys->default_clut.c_4b[i].T = T;
550 /* 256 entries CLUT */
551 memset( p_sys->default_clut.c_8b, 0xFF, 256 * sizeof(dvbsub_color_t) );
554 static void decode_segment( decoder_t *p_dec, bs_t *s )
556 decoder_sys_t *p_sys = p_dec->p_sys;
557 int i_type;
558 int i_page_id;
559 int i_size;
561 /* sync_byte (already checked) */
562 //bs_skip( s, 8 );
564 /* segment type */
565 i_type = bs_read( s, 8 );
567 /* page id */
568 i_page_id = bs_read( s, 16 );
570 /* segment size */
571 i_size = bs_read( s, 16 );
573 if( ( i_page_id != p_sys->i_id ) &&
574 ( i_page_id != p_sys->i_ancillary_id ) )
576 #ifdef DEBUG_DVBSUB
577 msg_Dbg( p_dec, "subtitle skipped (page id: %i, %i)",
578 i_page_id, p_sys->i_id );
579 #endif
580 bs_skip( s, 8 * i_size );
581 return;
584 if( ( p_sys->i_ancillary_id != p_sys->i_id ) &&
585 ( i_type == DVBSUB_ST_PAGE_COMPOSITION ) &&
586 ( i_page_id == p_sys->i_ancillary_id ) )
588 #ifdef DEBUG_DVBSUB
589 msg_Dbg( p_dec, "skipped invalid ancillary subtitle packet" );
590 #endif
591 bs_skip( s, 8 * i_size );
592 return;
595 #ifdef DEBUG_DVBSUB
596 if( i_page_id == p_sys->i_id )
597 msg_Dbg( p_dec, "segment (id: %i)", i_page_id );
598 else
599 msg_Dbg( p_dec, "ancillary segment (id: %i)", i_page_id );
600 #endif
602 switch( i_type )
604 case DVBSUB_ST_PAGE_COMPOSITION:
605 #ifdef DEBUG_DVBSUB
606 msg_Dbg( p_dec, "decode_page_composition" );
607 #endif
608 decode_page_composition( p_dec, s, i_size );
609 break;
611 case DVBSUB_ST_REGION_COMPOSITION:
612 #ifdef DEBUG_DVBSUB
613 msg_Dbg( p_dec, "decode_region_composition" );
614 #endif
615 decode_region_composition( p_dec, s, i_size );
616 break;
618 case DVBSUB_ST_CLUT_DEFINITION:
619 #ifdef DEBUG_DVBSUB
620 msg_Dbg( p_dec, "decode_clut" );
621 #endif
622 decode_clut( p_dec, s, i_size );
623 break;
625 case DVBSUB_ST_OBJECT_DATA:
626 #ifdef DEBUG_DVBSUB
627 msg_Dbg( p_dec, "decode_object" );
628 #endif
629 decode_object( p_dec, s, i_size );
630 break;
632 case DVBSUB_ST_DISPLAY_DEFINITION:
633 #ifdef DEBUG_DVBSUB
634 msg_Dbg( p_dec, "decode_display_definition" );
635 #endif
636 decode_display_definition( p_dec, s, i_size );
637 break;
639 case DVBSUB_ST_ENDOFDISPLAY:
640 #ifdef DEBUG_DVBSUB
641 msg_Dbg( p_dec, "end of display" );
642 #endif
643 bs_skip( s, 8 * i_size );
644 break;
646 case DVBSUB_ST_STUFFING:
647 #ifdef DEBUG_DVBSUB
648 msg_Dbg( p_dec, "skip stuffing" );
649 #endif
650 bs_skip( s, 8 * i_size );
651 break;
653 default:
654 msg_Warn( p_dec, "unsupported segment type: (%04x)", i_type );
655 bs_skip( s, 8 * i_size );
656 break;
660 static void decode_clut( decoder_t *p_dec, bs_t *s, uint16_t i_segment_length )
662 decoder_sys_t *p_sys = p_dec->p_sys;
663 uint16_t i_processed_length;
664 dvbsub_clut_t *p_clut, *p_next;
665 int i_id, i_version;
667 i_id = bs_read( s, 8 );
668 i_version = bs_read( s, 4 );
670 /* Check if we already have this clut */
671 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next )
673 if( p_clut->i_id == i_id ) break;
676 /* Check version number */
677 if( p_clut && ( p_clut->i_version == i_version ) )
679 /* Nothing to do */
680 bs_skip( s, 8 * i_segment_length - 12 );
681 return;
684 if( !p_clut )
686 #ifdef DEBUG_DVBSUB
687 msg_Dbg( p_dec, "new clut: %i", i_id );
688 #endif
689 p_clut = malloc( sizeof( dvbsub_clut_t ) );
690 if( !p_clut )
691 return;
692 p_clut->p_next = p_sys->p_cluts;
693 p_sys->p_cluts = p_clut;
696 /* Initialize to default clut */
697 p_next = p_clut->p_next;
698 *p_clut = p_sys->default_clut;
699 p_clut->p_next = p_next;
701 /* We don't have this version of the CLUT: Parse it */
702 p_clut->i_version = i_version;
703 p_clut->i_id = i_id;
704 bs_skip( s, 4 ); /* Reserved bits */
705 i_processed_length = 2;
706 while( i_processed_length < i_segment_length )
708 uint8_t y, cb, cr, t;
709 uint_fast8_t cid = bs_read( s, 8 );
710 uint_fast8_t type = bs_read( s, 3 );
712 bs_skip( s, 4 );
714 if( bs_read( s, 1 ) )
716 y = bs_read( s, 8 );
717 cr = bs_read( s, 8 );
718 cb = bs_read( s, 8 );
719 t = bs_read( s, 8 );
720 i_processed_length += 6;
722 else
724 y = bs_read( s, 6 ) << 2;
725 cr = bs_read( s, 4 ) << 4;
726 cb = bs_read( s, 4 ) << 4;
727 t = bs_read( s, 2 ) << 6;
728 i_processed_length += 4;
731 /* We are not entirely compliant here as full transparency is indicated
732 * with a luma value of zero, not a transparency value of 0xff
733 * (full transparency would actually be 0xff + 1). */
734 if( y == 0 )
736 cr = cb = 0;
737 t = 0xff;
740 /* According to EN 300-743 section 7.2.3 note 1, type should
741 * not have more than 1 bit set to one, but some streams don't
742 * respect this note. */
743 if( ( type & 0x04 ) && ( cid < 4 ) )
745 p_clut->c_2b[cid].Y = y;
746 p_clut->c_2b[cid].Cr = cr;
747 p_clut->c_2b[cid].Cb = cb;
748 p_clut->c_2b[cid].T = t;
750 if( ( type & 0x02 ) && ( cid < 16 ) )
752 p_clut->c_4b[cid].Y = y;
753 p_clut->c_4b[cid].Cr = cr;
754 p_clut->c_4b[cid].Cb = cb;
755 p_clut->c_4b[cid].T = t;
757 if( type & 0x01 )
759 p_clut->c_8b[cid].Y = y;
760 p_clut->c_8b[cid].Cr = cr;
761 p_clut->c_8b[cid].Cb = cb;
762 p_clut->c_8b[cid].T = t;
767 static void decode_page_composition( decoder_t *p_dec, bs_t *s, uint16_t i_segment_length )
769 decoder_sys_t *p_sys = p_dec->p_sys;
770 int i_version, i_state, i_timeout, i;
772 /* A page is composed by 0 or more region */
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, uint16_t i_segment_length )
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_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_id = bs_read( s, 8 );
868 i_version = bs_read( s, 4 );
870 /* Check if we already have this region */
871 for( p_region = p_sys->p_regions; p_region != NULL;
872 p_region = p_region->p_next )
874 pp_region = &p_region->p_next;
875 if( p_region->i_id == i_id ) break;
878 /* Check version number */
879 if( p_region && ( p_region->i_version == i_version ) )
881 bs_skip( s, 8 * (i_segment_length - 1) - 4 );
882 return;
885 if( !p_region )
887 #ifdef DEBUG_DVBSUB
888 msg_Dbg( p_dec, "new region: %i", i_id );
889 #endif
890 p_region = *pp_region = calloc( 1, sizeof(dvbsub_region_t) );
891 if( !p_region )
892 return;
893 p_region->p_object_defs = NULL;
894 p_region->p_pixbuf = NULL;
895 p_region->p_next = NULL;
898 /* Region attributes */
899 p_region->i_id = i_id;
900 p_region->i_version = i_version;
901 b_fill = bs_read( s, 1 );
902 bs_skip( s, 3 ); /* Reserved */
904 i_width = bs_read( s, 16 );
905 i_height = bs_read( s, 16 );
906 #ifdef DEBUG_DVBSUB
907 msg_Dbg( p_dec, " width=%d height=%d", i_width, i_height );
908 #endif
909 i_level_comp = bs_read( s, 3 );
910 i_depth = bs_read( s, 3 );
911 bs_skip( s, 2 ); /* Reserved */
912 i_clut = bs_read( s, 8 );
914 i_8_bg = bs_read( s, 8 );
915 i_4_bg = bs_read( s, 4 );
916 i_2_bg = bs_read( s, 2 );
917 bs_skip( s, 2 ); /* Reserved */
919 /* Free old object defs */
920 while( p_region->i_object_defs )
921 free( p_region->p_object_defs[--p_region->i_object_defs].psz_text );
923 free( p_region->p_object_defs );
924 p_region->p_object_defs = NULL;
926 /* Extra sanity checks */
927 if( ( p_region->i_width != i_width ) ||
928 ( p_region->i_height != i_height ) )
930 if( p_region->p_pixbuf )
932 msg_Dbg( p_dec, "region size changed (%dx%d->%dx%d)",
933 p_region->i_width, p_region->i_height, i_width, i_height );
934 free( p_region->p_pixbuf );
937 p_region->p_pixbuf = xmalloc( i_height * i_width );
938 p_region->i_depth = 0;
939 b_fill = true;
941 if( p_region->i_depth &&
942 ( ( p_region->i_depth != i_depth ) ||
943 ( p_region->i_level_comp != i_level_comp ) ||
944 ( p_region->i_clut != i_clut) ) )
946 msg_Dbg( p_dec, "region parameters changed (not allowed)" );
949 /* Erase background of region */
950 if( b_fill )
952 int i_background = ( i_depth == 1 ) ? i_2_bg :
953 ( ( i_depth == 2 ) ? i_4_bg : i_8_bg );
954 memset( p_region->p_pixbuf, i_background, i_width * i_height );
957 p_region->i_width = i_width;
958 p_region->i_height = i_height;
959 p_region->i_level_comp = i_level_comp;
960 p_region->i_depth = i_depth;
961 p_region->i_clut = i_clut;
963 /* List of objects in the region */
964 i_processed_length = 10;
965 while( i_processed_length < i_segment_length )
967 dvbsub_objectdef_t *p_obj;
969 /* We create a new object */
970 p_region->i_object_defs++;
971 p_region->p_object_defs = xrealloc( p_region->p_object_defs,
972 sizeof(dvbsub_objectdef_t) * p_region->i_object_defs );
974 /* We parse object properties */
975 p_obj = &p_region->p_object_defs[p_region->i_object_defs - 1];
976 p_obj->i_id = bs_read( s, 16 );
977 p_obj->i_type = bs_read( s, 2 );
978 bs_skip( s, 2 ); /* Provider */
979 p_obj->i_x = bs_read( s, 12 );
980 bs_skip( s, 4 ); /* Reserved */
981 p_obj->i_y = bs_read( s, 12 );
982 p_obj->psz_text = NULL;
984 i_processed_length += 6;
986 if( ( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ) ||
987 ( p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING ) )
989 p_obj->i_fg_pc = bs_read( s, 8 );
990 p_obj->i_bg_pc = bs_read( s, 8 );
991 i_processed_length += 2;
996 /* ETSI 300 743 [7.2.1] */
997 static void decode_display_definition( decoder_t *p_dec, bs_t *s, uint16_t i_segment_length )
999 decoder_sys_t *p_sys = p_dec->p_sys;
1000 uint16_t i_processed_length = 40;
1001 int i_version;
1003 i_version = bs_read( s, 4 );
1005 /* Check version number */
1006 if( p_sys->display.i_version == i_version )
1008 /* The definition did not change */
1009 bs_skip( s, 8*i_segment_length - 4 );
1010 return;
1013 #ifdef DEBUG_DVBSUB
1014 msg_Dbg( p_dec, "new display definition: %i", i_version );
1015 #endif
1017 /* We don't have this version of the display definition: Parse it */
1018 p_sys->display.i_version = i_version;
1019 p_sys->display.b_windowed = bs_read( s, 1 );
1020 bs_skip( s, 3 ); /* Reserved bits */
1021 p_sys->display.i_width = bs_read( s, 16 )+1;
1022 p_sys->display.i_height = bs_read( s, 16 )+1;
1024 if( p_sys->display.b_windowed )
1026 #ifdef DEBUG_DVBSUB
1027 msg_Dbg( p_dec, "display definition with offsets (windowed)" );
1028 #endif
1029 /* Coordinates are measured from the top left corner */
1030 p_sys->display.i_x = bs_read( s, 16 );
1031 p_sys->display.i_max_x = bs_read( s, 16 );
1032 p_sys->display.i_y = bs_read( s, 16 );
1033 p_sys->display.i_max_y = bs_read( s, 16 );
1034 i_processed_length += 64;
1036 else
1038 /* if not windowed, setup the window variables to good defaults */
1039 /* not necessary, but to avoid future confusion.. */
1040 p_sys->display.i_x = 0;
1041 p_sys->display.i_max_x = p_sys->display.i_width-1;
1042 p_sys->display.i_y = 0;
1043 p_sys->display.i_max_y = p_sys->display.i_height-1;
1046 if( i_processed_length != i_segment_length*8 )
1048 msg_Err( p_dec, "processed length %d bytes != segment length %d bytes",
1049 i_processed_length / 8 , i_segment_length );
1052 #ifdef DEBUG_DVBSUB
1053 msg_Dbg( p_dec, "version: %d, width: %d, height: %d",
1054 p_sys->display.i_version, p_sys->display.i_width, p_sys->display.i_height );
1055 if( p_sys->display.b_windowed )
1056 msg_Dbg( p_dec, "xmin: %d, xmax: %d, ymin: %d, ymax: %d",
1057 p_sys->display.i_x, p_sys->display.i_max_x, p_sys->display.i_y, p_sys->display.i_max_y );
1058 #endif
1061 static void dvbsub_render_pdata( decoder_t *, dvbsub_region_t *, int, int,
1062 uint8_t *, int );
1063 static void dvbsub_pdata2bpp( bs_t *, uint8_t *, int, int * );
1064 static void dvbsub_pdata4bpp( bs_t *, uint8_t *, int, int * );
1065 static void dvbsub_pdata8bpp( bs_t *, uint8_t *, int, int * );
1067 static void decode_object( decoder_t *p_dec, bs_t *s, uint16_t i_segment_length )
1069 decoder_sys_t *p_sys = p_dec->p_sys;
1070 dvbsub_region_t *p_region;
1071 int i_coding_method, i_id, i;
1073 /* ETSI 300-743 paragraph 7.2.4
1074 * sync_byte, segment_type and page_id have already been processed.
1076 i_id = bs_read( s, 16 );
1077 bs_skip( s, 4 ); /* version */
1078 i_coding_method = bs_read( s, 2 );
1080 if( i_coding_method > 1 )
1082 msg_Dbg( p_dec, "unknown DVB subtitling coding %d is not handled!", i_coding_method );
1083 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
1084 return;
1087 /* Check if the object needs to be rendered in at least one
1088 * of the regions */
1089 for( p_region = p_sys->p_regions; p_region != NULL;
1090 p_region = p_region->p_next )
1092 for( i = 0; i < p_region->i_object_defs; i++ )
1093 if( p_region->p_object_defs[i].i_id == i_id ) break;
1095 if( i != p_region->i_object_defs ) break;
1097 if( !p_region )
1099 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
1100 return;
1103 #ifdef DEBUG_DVBSUB
1104 msg_Dbg( p_dec, "new object: %i", i_id );
1105 #endif
1107 bs_skip( s, 1 ); /* non_modify_color */
1108 bs_skip( s, 1 ); /* Reserved */
1110 if( i_coding_method == 0x00 )
1112 int i_topfield, i_bottomfield;
1113 uint8_t *p_topfield, *p_bottomfield;
1115 i_topfield = bs_read( s, 16 );
1116 i_bottomfield = bs_read( s, 16 );
1117 p_topfield = s->p_start + bs_pos( s ) / 8;
1118 p_bottomfield = p_topfield + i_topfield;
1120 bs_skip( s, 8 * (i_segment_length - 7) );
1122 /* Sanity check */
1123 if( ( i_segment_length < ( i_topfield + i_bottomfield + 7 ) ) ||
1124 ( ( p_topfield + i_topfield + i_bottomfield ) > s->p_end ) )
1126 msg_Dbg( p_dec, "corrupted object data" );
1127 return;
1130 for( p_region = p_sys->p_regions; p_region != NULL;
1131 p_region = p_region->p_next )
1133 for( i = 0; i < p_region->i_object_defs; i++ )
1135 if( p_region->p_object_defs[i].i_id != i_id ) continue;
1137 dvbsub_render_pdata( p_dec, p_region,
1138 p_region->p_object_defs[i].i_x,
1139 p_region->p_object_defs[i].i_y,
1140 p_topfield, i_topfield );
1142 if( i_bottomfield )
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 + 1,
1147 p_bottomfield, i_bottomfield );
1149 else
1151 /* Duplicate the top field */
1152 dvbsub_render_pdata( p_dec, p_region,
1153 p_region->p_object_defs[i].i_x,
1154 p_region->p_object_defs[i].i_y + 1,
1155 p_topfield, i_topfield );
1160 else
1162 /* DVB subtitling as characters */
1163 int i_number_of_codes = bs_read( s, 8 );
1164 uint8_t* p_start = s->p_start + bs_pos( s ) / 8;
1166 /* Sanity check */
1167 if( ( i_segment_length < ( i_number_of_codes*2 + 4 ) ) ||
1168 ( ( p_start + i_number_of_codes*2 ) > s->p_end ) )
1170 msg_Dbg( p_dec, "corrupted object data" );
1171 return;
1174 for( p_region = p_sys->p_regions; p_region != NULL;
1175 p_region = p_region->p_next )
1177 for( i = 0; i < p_region->i_object_defs; i++ )
1179 int j;
1181 if( p_region->p_object_defs[i].i_id != i_id ) continue;
1183 p_region->p_object_defs[i].psz_text =
1184 xrealloc( p_region->p_object_defs[i].psz_text,
1185 i_number_of_codes + 1 );
1187 /* FIXME 16bits -> char ??? See Preamble */
1188 for( j = 0; j < i_number_of_codes; j++ )
1190 p_region->p_object_defs[i].psz_text[j] = (char)(bs_read( s, 16 ) & 0xFF);
1192 /* Null terminate the string */
1193 p_region->p_object_defs[i].psz_text[j] = 0;
1198 #ifdef DEBUG_DVBSUB
1199 msg_Dbg( p_dec, "end object: %i", i_id );
1200 #endif
1203 static void dvbsub_render_pdata( decoder_t *p_dec, dvbsub_region_t *p_region,
1204 int i_x, int i_y,
1205 uint8_t *p_field, int i_field )
1207 uint8_t *p_pixbuf;
1208 int i_offset = 0;
1209 bs_t bs;
1211 /* Sanity check */
1212 if( !p_region->p_pixbuf )
1214 msg_Err( p_dec, "region %i has no pixel buffer!", p_region->i_id );
1215 return;
1217 if( i_y < 0 || i_x < 0 || i_y >= p_region->i_height ||
1218 i_x >= p_region->i_width )
1220 msg_Dbg( p_dec, "invalid offset (%i,%i)", i_x, i_y );
1221 return;
1224 p_pixbuf = p_region->p_pixbuf + i_y * p_region->i_width;
1225 bs_init( &bs, p_field, i_field );
1227 while( !bs_eof( &bs ) )
1229 /* Sanity check */
1230 if( i_y >= p_region->i_height ) return;
1232 switch( bs_read( &bs, 8 ) )
1234 case 0x10:
1235 dvbsub_pdata2bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1236 &i_offset );
1237 break;
1239 case 0x11:
1240 dvbsub_pdata4bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1241 &i_offset );
1242 break;
1244 case 0x12:
1245 dvbsub_pdata8bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1246 &i_offset );
1247 break;
1249 case 0x20:
1250 case 0x21:
1251 case 0x22:
1252 /* We don't use map tables */
1253 break;
1255 case 0xf0: /* End of line code */
1256 p_pixbuf += 2*p_region->i_width;
1257 i_offset = 0; i_y += 2;
1258 break;
1263 static void dvbsub_pdata2bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1265 bool b_stop = false;
1267 while( !b_stop && !bs_eof( s ) )
1269 int i_count = 0, i_color = 0;
1271 i_color = bs_read( s, 2 );
1272 if( i_color != 0x00 )
1274 i_count = 1;
1276 else
1278 if( bs_read( s, 1 ) == 0x01 ) // Switch1
1280 i_count = 3 + bs_read( s, 3 );
1281 i_color = bs_read( s, 2 );
1283 else
1285 if( bs_read( s, 1 ) == 0x00 ) //Switch2
1287 switch( bs_read( s, 2 ) ) //Switch3
1289 case 0x00:
1290 b_stop = true;
1291 break;
1292 case 0x01:
1293 i_count = 2;
1294 break;
1295 case 0x02:
1296 i_count = 12 + bs_read( s, 4 );
1297 i_color = bs_read( s, 2 );
1298 break;
1299 case 0x03:
1300 i_count = 29 + bs_read( s, 8 );
1301 i_color = bs_read( s, 2 );
1302 break;
1303 default:
1304 break;
1307 else
1309 /* 1 pixel color 0 */
1310 i_count = 1;
1315 if( !i_count ) continue;
1317 /* Sanity check */
1318 if( ( i_count + *pi_off ) > i_width ) break;
1320 if( i_count == 1 ) p[*pi_off] = i_color;
1321 else memset( ( p + *pi_off ), i_color, i_count );
1323 (*pi_off) += i_count;
1326 bs_align( s );
1329 static void dvbsub_pdata4bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1331 bool b_stop = false;
1333 while( !b_stop && !bs_eof( s ) )
1335 int i_count = 0, i_color = 0;
1337 i_color = bs_read( s, 4 );
1338 if( i_color != 0x00 )
1340 /* Add 1 pixel */
1341 i_count = 1;
1343 else
1345 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1347 i_count = bs_read( s, 3 );
1348 if( i_count != 0x00 )
1350 i_count += 2;
1352 else b_stop = true;
1354 else
1356 if( bs_read( s, 1 ) == 0x00) //Switch2
1358 i_count = 4 + bs_read( s, 2 );
1359 i_color = bs_read( s, 4 );
1361 else
1363 switch ( bs_read( s, 2 ) ) //Switch3
1365 case 0x0:
1366 i_count = 1;
1367 break;
1368 case 0x1:
1369 i_count = 2;
1370 break;
1371 case 0x2:
1372 i_count = 9 + bs_read( s, 4 );
1373 i_color = bs_read( s, 4 );
1374 break;
1375 case 0x3:
1376 i_count= 25 + bs_read( s, 8 );
1377 i_color = bs_read( s, 4 );
1378 break;
1384 if( !i_count ) continue;
1386 /* Sanity check */
1387 if( ( i_count + *pi_off ) > i_width ) break;
1389 if( i_count == 1 ) p[*pi_off] = i_color;
1390 else memset( ( p + *pi_off ), i_color, i_count );
1392 (*pi_off) += i_count;
1395 bs_align( s );
1398 static void dvbsub_pdata8bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1400 bool b_stop = false;
1402 while( !b_stop && !bs_eof( s ) )
1404 int i_count = 0, i_color = 0;
1406 i_color = bs_read( s, 8 );
1407 if( i_color != 0x00 )
1409 /* Add 1 pixel */
1410 i_count = 1;
1412 else
1414 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1416 i_count = bs_read( s, 7 );
1417 if( i_count == 0x00 )
1418 b_stop = true;
1420 else
1422 i_count = bs_read( s, 7 );
1423 i_color = bs_read( s, 8 );
1427 if( !i_count ) continue;
1429 /* Sanity check */
1430 if( ( i_count + *pi_off ) > i_width ) break;
1432 if( i_count == 1 ) p[*pi_off] = i_color;
1433 else memset( ( p + *pi_off ), i_color, i_count );
1435 (*pi_off) += i_count;
1438 bs_align( s );
1441 static void free_all( decoder_t *p_dec )
1443 decoder_sys_t *p_sys = p_dec->p_sys;
1444 dvbsub_region_t *p_reg, *p_reg_next;
1445 dvbsub_clut_t *p_clut, *p_clut_next;
1447 /*free( p_sys->p_display ); No longer malloced */
1449 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut_next )
1451 p_clut_next = p_clut->p_next;
1452 free( p_clut );
1454 p_sys->p_cluts = NULL;
1456 for( p_reg = p_sys->p_regions; p_reg != NULL; p_reg = p_reg_next )
1458 p_reg_next = p_reg->p_next;
1459 for( int i = 0; i < p_reg->i_object_defs; i++ )
1460 free( p_reg->p_object_defs[i].psz_text );
1461 if( p_reg->i_object_defs ) free( p_reg->p_object_defs );
1462 free( p_reg->p_pixbuf );
1463 free( p_reg );
1465 p_sys->p_regions = NULL;
1467 if( p_sys->p_page )
1469 if( p_sys->p_page->i_region_defs )
1470 free( p_sys->p_page->p_region_defs );
1471 free( p_sys->p_page );
1473 p_sys->p_page = NULL;
1476 static subpicture_t *render( decoder_t *p_dec )
1478 decoder_sys_t *p_sys = p_dec->p_sys;
1479 subpicture_t *p_spu;
1480 subpicture_region_t **pp_spu_region;
1481 int i, j;
1482 int i_base_x;
1483 int i_base_y;
1485 /* Allocate the subpicture internal data. */
1486 p_spu = decoder_NewSubpicture( p_dec, NULL );
1487 if( !p_spu )
1488 return NULL;
1490 p_spu->b_absolute = p_sys->b_absolute;
1491 /* Set the pf_render callback */
1492 p_spu->i_start = p_sys->i_pts;
1493 //p_spu->i_stop = (vlc_tick_t) 0;
1494 p_spu->b_ephemer = true;
1495 //p_spu->b_fade = true;
1496 //p_spu->i_stop = p_spu->i_start + (vlc_tick_t) (i_timeout * 1000000);
1497 p_spu->b_subtitle = true;
1499 /* Correct positioning of SPU */
1500 i_base_x = p_sys->i_spu_x;
1501 i_base_y = p_sys->i_spu_y;
1503 p_spu->i_original_picture_width = p_sys->display.i_width;
1504 p_spu->i_original_picture_height = p_sys->display.i_height;
1506 if( p_sys->display.b_windowed )
1508 /* From en_300743v01 - */
1509 /* the DDS is there to indicate intended size/position of text */
1510 /* the intended video area is ->i_width/height */
1511 /* the window is within this... SO... we should leave i_original_picture_width etc. as is */
1512 /* and ONLY change i_base_x. effectively i_max_x/y are only there to limit memory requirements*/
1513 /* we COULD use the upper limits to limit rendering to within these? */
1515 /* see notes on DDS at the top of the file */
1516 i_base_x += p_sys->display.i_x;
1517 i_base_y += p_sys->display.i_y;
1520 pp_spu_region = &p_spu->p_region;
1522 /* Loop on region definitions */
1523 #ifdef DEBUG_DVBSUB
1524 if( p_sys->p_page )
1525 msg_Dbg( p_dec, "rendering %i regions", p_sys->p_page->i_region_defs );
1526 #endif
1528 for( i = 0; p_sys->p_page && ( i < p_sys->p_page->i_region_defs ); i++ )
1530 dvbsub_region_t *p_region;
1531 dvbsub_regiondef_t *p_regiondef;
1532 dvbsub_clut_t *p_clut;
1533 dvbsub_color_t *p_color;
1534 subpicture_region_t *p_spu_region;
1535 uint8_t *p_src, *p_dst;
1536 video_format_t fmt;
1537 video_palette_t palette;
1538 int i_pitch;
1540 p_regiondef = &p_sys->p_page->p_region_defs[i];
1542 /* Find associated region */
1543 for( p_region = p_sys->p_regions; p_region != NULL;
1544 p_region = p_region->p_next )
1546 if( p_regiondef->i_id == p_region->i_id ) break;
1549 #ifdef DEBUG_DVBSUB
1550 /* if a region exists, then print it's size */
1551 if (p_region)
1553 msg_Dbg( p_dec, "rendering region %i (%i,%i) to (%i,%i)", i,
1554 p_regiondef->i_x, p_regiondef->i_y,
1555 p_regiondef->i_x + p_region->i_width,
1556 p_regiondef->i_y + p_region->i_height );
1558 else
1560 msg_Dbg( p_dec, "rendering region %i (%i,%i) (no region matched to render)", i,
1561 p_regiondef->i_x, p_regiondef->i_y );
1563 #endif
1565 if( !p_region )
1567 msg_Dbg( p_dec, "region %i not found", p_regiondef->i_id );
1568 continue;
1571 /* Find associated CLUT */
1572 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next )
1574 if( p_region->i_clut == p_clut->i_id ) break;
1576 if( !p_clut )
1578 msg_Dbg( p_dec, "clut %i not found", p_region->i_clut );
1579 continue;
1582 /* FIXME: don't create a subpicture region with VLC CODEC YUVP
1583 * when it actually is a TEXT region */
1585 /* Create new SPU region */
1586 video_format_Init( &fmt, VLC_CODEC_YUVP );
1587 fmt.i_sar_num = 0; /* 0 means use aspect ratio of background video */
1588 fmt.i_sar_den = 1;
1589 fmt.i_width = fmt.i_visible_width = p_region->i_width;
1590 fmt.i_height = fmt.i_visible_height = p_region->i_height;
1591 fmt.i_x_offset = fmt.i_y_offset = 0;
1592 fmt.p_palette = &palette;
1593 fmt.p_palette->i_entries = ( p_region->i_depth == 1 ) ? 4 :
1594 ( ( p_region->i_depth == 2 ) ? 16 : 256 );
1595 p_color = ( p_region->i_depth == 1 ) ? p_clut->c_2b :
1596 ( ( p_region->i_depth == 2 ) ? p_clut->c_4b : p_clut->c_8b );
1597 for( j = 0; j < fmt.p_palette->i_entries; j++ )
1599 fmt.p_palette->palette[j][0] = p_color[j].Y;
1600 fmt.p_palette->palette[j][1] = p_color[j].Cb; /* U == Cb */
1601 fmt.p_palette->palette[j][2] = p_color[j].Cr; /* V == Cr */
1602 fmt.p_palette->palette[j][3] = 0xff - p_color[j].T;
1605 p_spu_region = subpicture_region_New( &fmt );
1606 fmt.p_palette = NULL; /* was stack var */
1607 video_format_Clean( &fmt );
1608 if( !p_spu_region )
1610 msg_Err( p_dec, "cannot allocate SPU region" );
1611 continue;
1613 p_spu_region->i_x = i_base_x + p_regiondef->i_x;
1614 p_spu_region->i_y = i_base_y + p_regiondef->i_y;
1615 p_spu_region->i_align = p_sys->i_spu_position;
1616 *pp_spu_region = p_spu_region;
1617 pp_spu_region = &p_spu_region->p_next;
1619 p_src = p_region->p_pixbuf;
1620 p_dst = p_spu_region->p_picture->Y_PIXELS;
1621 i_pitch = p_spu_region->p_picture->Y_PITCH;
1623 /* Copy pixel buffer */
1624 for( j = 0; j < p_region->i_height; j++ )
1626 memcpy( p_dst, p_src, p_region->i_width );
1627 p_src += p_region->i_width;
1628 p_dst += i_pitch;
1631 /* Check subtitles encoded as strings of characters
1632 * (since there are not rendered in the pixbuffer) */
1633 for( j = 0; j < p_region->i_object_defs; j++ )
1635 dvbsub_objectdef_t *p_object_def = &p_region->p_object_defs[j];
1637 if( ( p_object_def->i_type != 1 ) || !p_object_def->psz_text )
1638 continue;
1640 /* Create new SPU region */
1641 video_format_Init( &fmt, VLC_CODEC_TEXT );
1642 fmt.i_sar_num = 1;
1643 fmt.i_sar_den = 1;
1644 fmt.i_width = fmt.i_visible_width = p_region->i_width;
1645 fmt.i_height = fmt.i_visible_height = p_region->i_height;
1646 fmt.i_x_offset = fmt.i_y_offset = 0;
1647 p_spu_region = subpicture_region_New( &fmt );
1648 video_format_Clean( &fmt );
1650 p_spu_region->p_text = text_segment_New( p_object_def->psz_text );
1651 p_spu_region->i_x = i_base_x + p_regiondef->i_x + p_object_def->i_x;
1652 p_spu_region->i_y = i_base_y + p_regiondef->i_y + p_object_def->i_y;
1653 p_spu_region->i_align = p_sys->i_spu_position;
1654 *pp_spu_region = p_spu_region;
1655 pp_spu_region = &p_spu_region->p_next;
1659 return p_spu;
1662 /*****************************************************************************
1663 * encoder_sys_t : encoder descriptor
1664 *****************************************************************************/
1665 typedef struct encoder_region_t
1667 int i_width;
1668 int i_height;
1670 } encoder_region_t;
1672 typedef struct
1674 unsigned int i_page_ver;
1675 unsigned int i_region_ver;
1676 unsigned int i_clut_ver;
1678 int i_regions;
1679 encoder_region_t *p_regions;
1681 vlc_tick_t i_pts;
1683 /* subpicture positioning */
1684 int i_offset_x;
1685 int i_offset_y;
1686 } encoder_sys_t;
1688 #ifdef ENABLE_SOUT
1689 static void encode_page_composition( encoder_t *, bs_t *, subpicture_t * );
1690 static void encode_clut( encoder_t *, bs_t *, subpicture_t * );
1691 static void encode_region_composition( encoder_t *, bs_t *, subpicture_t * );
1692 static void encode_object( encoder_t *, bs_t *, subpicture_t * );
1694 /*****************************************************************************
1695 * OpenEncoder: probe the encoder and return score
1696 *****************************************************************************/
1697 static int OpenEncoder( vlc_object_t *p_this )
1699 encoder_t *p_enc = (encoder_t *)p_this;
1700 encoder_sys_t *p_sys;
1702 if( ( p_enc->fmt_out.i_codec != VLC_CODEC_DVBS ) &&
1703 !p_enc->obj.force )
1705 return VLC_EGENERIC;
1708 /* Allocate the memory needed to store the decoder's structure */
1709 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1710 return VLC_ENOMEM;
1711 p_enc->p_sys = p_sys;
1713 p_enc->pf_encode_sub = Encode;
1714 p_enc->fmt_out.i_codec = VLC_CODEC_DVBS;
1715 p_enc->fmt_out.subs.dvb.i_id = 1 << 16 | 1;
1717 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
1719 p_sys->i_page_ver = 0;
1720 p_sys->i_region_ver = 0;
1721 p_sys->i_clut_ver = 0;
1722 p_sys->i_regions = 0;
1723 p_sys->p_regions = 0;
1725 p_sys->i_offset_x = var_CreateGetInteger( p_this, ENC_CFG_PREFIX "x" );
1726 p_sys->i_offset_y = var_CreateGetInteger( p_this, ENC_CFG_PREFIX "y" );
1728 return VLC_SUCCESS;
1731 /* FIXME: this routine is a hack to convert VLC_CODEC_YUVA
1732 * into VLC_CODEC_YUVP
1734 static subpicture_t *YuvaYuvp( subpicture_t *p_subpic )
1736 subpicture_region_t *p_region = NULL;
1738 if( !p_subpic ) return NULL;
1740 for( p_region = p_subpic->p_region; p_region; p_region = p_region->p_next )
1742 video_format_t *p_fmt = &p_region->fmt;
1743 int i = 0, j = 0, n = 0, p = 0;
1744 int i_max_entries = 256;
1746 #ifdef RANDOM_DITHERING
1747 int i_seed = 0xdeadbeef; /* random seed */
1748 #else
1749 int *pi_delta;
1750 #endif
1751 int i_pixels = p_region->p_picture->p[0].i_visible_lines
1752 * p_region->p_picture->p[0].i_pitch;
1753 int i_iterator = p_region->p_picture->p[0].i_visible_lines * 3 / 4
1754 * p_region->p_picture->p[0].i_pitch
1755 + p_region->p_picture->p[0].i_pitch * 1 / 3;
1756 int i_tolerance = 0;
1758 #ifdef DEBUG_DVBSUB1
1759 /* p_enc not valid here */
1760 msg_Dbg( p_enc, "YuvaYuvp: i_pixels=%d, i_iterator=%d", i_pixels, i_iterator );
1761 #endif
1762 p_fmt->i_chroma = VLC_CODEC_YUVP;
1763 p_fmt->p_palette = (video_palette_t *) malloc( sizeof( video_palette_t ) );
1764 if( !p_fmt->p_palette ) break;
1765 p_fmt->p_palette->i_entries = 0;
1767 /* Find best iterator using Euclide’s algorithm */
1768 for( ; i_iterator > 1 ; i_iterator-- )
1770 int a = i_pixels;
1771 int b = i_iterator;
1772 int c;
1774 while( b )
1776 c = a % b;
1777 a = b;
1778 b = c;
1781 if( a == 1 )
1783 break;
1787 /* Count colors, build best palette */
1788 for( i_tolerance = 0; i_tolerance < 128; i_tolerance++ )
1790 bool b_success = true;
1791 p_fmt->p_palette->i_entries = 0;
1793 for( i = 0; i < i_pixels ; )
1795 uint8_t y, u, v, a;
1796 y = p_region->p_picture->p[0].p_pixels[i];
1797 u = p_region->p_picture->p[1].p_pixels[i];
1798 v = p_region->p_picture->p[2].p_pixels[i];
1799 a = p_region->p_picture->p[3].p_pixels[i];
1800 for( j = 0; j < p_fmt->p_palette->i_entries; j++ )
1802 if( abs((int)p_fmt->p_palette->palette[j][0] - (int)y) <= i_tolerance &&
1803 abs((int)p_fmt->p_palette->palette[j][1] - (int)u) <= i_tolerance &&
1804 abs((int)p_fmt->p_palette->palette[j][2] - (int)v) <= i_tolerance &&
1805 abs((int)p_fmt->p_palette->palette[j][3] - (int)a) <= i_tolerance / 2 )
1807 break;
1810 if( j == p_fmt->p_palette->i_entries )
1812 p_fmt->p_palette->palette[j][0] = y;
1813 p_fmt->p_palette->palette[j][1] = u;
1814 p_fmt->p_palette->palette[j][2] = v;
1815 p_fmt->p_palette->palette[j][3] = a;
1816 p_fmt->p_palette->i_entries++;
1818 if( p_fmt->p_palette->i_entries >= i_max_entries )
1820 b_success = false;
1821 break;
1823 i += i_iterator;
1824 if( i > i_pixels )
1826 i -= i_pixels;
1830 if( b_success )
1832 break;
1836 #ifdef DEBUG_DVBSUB1
1837 /* p_enc not valid here */
1838 msg_Dbg( p_enc, "best palette has %d colors", p_fmt->p_palette->i_entries );
1839 #endif
1841 #ifndef RANDOM_DITHERING
1842 pi_delta = xmalloc( ( p_region->p_picture->p[0].i_pitch + 1 )
1843 * sizeof(int) * 4 );
1844 for( i = 0; i < (p_region->p_picture->p[0].i_pitch + 1) * 4 ; i++ )
1846 pi_delta[ i ] = 0;
1848 #endif
1850 /* Fill image with our new colours */
1851 for( p = 0; p < p_region->p_picture->p[0].i_visible_lines ; p++ )
1853 int i_ydelta = 0, i_udelta = 0, i_vdelta = 0, i_adelta = 0;
1855 for( n = 0; n < p_region->p_picture->p[0].i_pitch ; n++ )
1857 int i_offset = p * p_region->p_picture->p[0].i_pitch + n;
1858 int y, u, v, a;
1859 int i_mindist, i_best;
1861 y = (int)p_region->p_picture->p[0].p_pixels[i_offset];
1862 u = (int)p_region->p_picture->p[1].p_pixels[i_offset];
1863 v = (int)p_region->p_picture->p[2].p_pixels[i_offset];
1864 a = (int)p_region->p_picture->p[3].p_pixels[i_offset];
1866 /* Add dithering compensation */
1867 #ifdef RANDOM_DITHERING
1868 y += ((i_seed & 0xff) - 0x80) * i_tolerance / 0x80;
1869 u += (((i_seed >> 8) & 0xff) - 0x80) * i_tolerance / 0x80;
1870 v += (((i_seed >> 16) & 0xff) - 0x80) * i_tolerance / 0x80;
1871 a += (((i_seed >> 24) & 0xff) - 0x80) * i_tolerance / 0x80;
1872 #else
1873 y += i_ydelta + pi_delta[ n * 4 ];
1874 u += i_udelta + pi_delta[ n * 4 + 1 ];
1875 v += i_vdelta + pi_delta[ n * 4 + 2 ];
1876 a += i_adelta + pi_delta[ n * 4 + 3 ];
1877 #endif
1879 /* Find best colour in palette */
1880 for( i_mindist = 99999999, i_best = 0, j = 0; j < p_fmt->p_palette->i_entries; j++ )
1882 int i_dist = 0;
1884 i_dist += abs((int)p_fmt->p_palette->palette[j][0] - y);
1885 i_dist += abs((int)p_fmt->p_palette->palette[j][1] - u);
1886 i_dist += abs((int)p_fmt->p_palette->palette[j][2] - v);
1887 i_dist += 2 * abs((int)p_fmt->p_palette->palette[j][3] - a);
1889 if( i_dist < i_mindist )
1891 i_mindist = i_dist;
1892 i_best = j;
1896 /* Set pixel to best color */
1897 p_region->p_picture->p[0].p_pixels[i_offset] = i_best;
1899 /* Update dithering state */
1900 #ifdef RANDOM_DITHERING
1901 i_seed = (i_seed * 0x1283837) ^ 0x789479 ^ (i_seed >> 13);
1902 #else
1903 i_ydelta = y - (int)p_fmt->p_palette->palette[i_best][0];
1904 i_udelta = u - (int)p_fmt->p_palette->palette[i_best][1];
1905 i_vdelta = v - (int)p_fmt->p_palette->palette[i_best][2];
1906 i_adelta = a - (int)p_fmt->p_palette->palette[i_best][3];
1907 pi_delta[ n * 4 ] = i_ydelta * 3 / 8;
1908 pi_delta[ n * 4 + 1 ] = i_udelta * 3 / 8;
1909 pi_delta[ n * 4 + 2 ] = i_vdelta * 3 / 8;
1910 pi_delta[ n * 4 + 3 ] = i_adelta * 3 / 8;
1911 i_ydelta = i_ydelta * 5 / 8;
1912 i_udelta = i_udelta * 5 / 8;
1913 i_vdelta = i_vdelta * 5 / 8;
1914 i_adelta = i_adelta * 5 / 8;
1915 #endif
1918 #ifndef RANDOM_DITHERING
1919 free( pi_delta );
1920 #endif
1922 /* pad palette */
1923 for( i = p_fmt->p_palette->i_entries; i < i_max_entries; i++ )
1925 p_fmt->p_palette->palette[i][0] = 0;
1926 p_fmt->p_palette->palette[i][1] = 0;
1927 p_fmt->p_palette->palette[i][2] = 0;
1928 p_fmt->p_palette->palette[i][3] = 0;
1930 p_fmt->p_palette->i_entries = i_max_entries;
1931 #ifdef DEBUG_DVBSUB1
1932 /* p_enc not valid here */
1933 msg_Dbg( p_enc, "best palette has %d colors", p_fmt->p_palette->i_entries );
1934 #endif
1936 return p_subpic;
1937 } /* End of hack */
1939 /****************************************************************************
1940 * Encode: the whole thing
1941 ****************************************************************************/
1942 static block_t *Encode( encoder_t *p_enc, subpicture_t *p_subpic )
1944 subpicture_t *p_temp = NULL;
1945 subpicture_region_t *p_region = NULL;
1946 bs_t bits, *s = &bits;
1947 block_t *p_block;
1949 if( !p_subpic || !p_subpic->p_region ) return NULL;
1951 /* FIXME: this is a hack to convert VLC_CODEC_YUVA into
1952 * VLC_CODEC_YUVP
1954 p_region = p_subpic->p_region;
1955 if( p_region->fmt.i_chroma == VLC_CODEC_YUVA )
1957 p_temp = YuvaYuvp( p_subpic );
1958 if( !p_temp )
1960 msg_Err( p_enc, "no picture in subpicture" );
1961 return NULL;
1963 p_region = p_subpic->p_region;
1966 /* Sanity check */
1967 if( !p_region ) return NULL;
1969 if( ( p_region->fmt.i_chroma != VLC_CODEC_TEXT ) &&
1970 ( p_region->fmt.i_chroma != VLC_CODEC_YUVP ) )
1972 msg_Err( p_enc, "chroma %4.4s not supported", (char *)&p_region->fmt.i_chroma );
1973 return NULL;
1976 if( p_region->fmt.p_palette )
1978 switch( p_region->fmt.p_palette->i_entries )
1980 case 0:
1981 case 4:
1982 case 16:
1983 case 256:
1984 break;
1985 default:
1986 msg_Err( p_enc, "subpicture palette (%d) not handled",
1987 p_region->fmt.p_palette->i_entries );
1988 return NULL;
1991 /* End of hack */
1993 #ifdef DEBUG_DVBSUB
1994 msg_Dbg( p_enc, "encoding subpicture" );
1995 #endif
1996 p_block = block_Alloc( 64000 );
1997 bs_init( s, p_block->p_buffer, p_block->i_buffer );
1999 bs_write( s, 8, 0x20 ); /* Data identifier */
2000 bs_write( s, 8, 0x0 ); /* Subtitle stream id */
2002 encode_page_composition( p_enc, s, p_subpic );
2003 encode_region_composition( p_enc, s, p_subpic );
2004 encode_clut( p_enc, s, p_subpic );
2005 encode_object( p_enc, s, p_subpic );
2007 /* End of display */
2008 bs_write( s, 8, 0x0f ); /* Sync byte */
2009 bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
2010 bs_write( s, 16, 1 ); /* Page id */
2011 bs_write( s, 16, 0 ); /* Segment length */
2013 bs_write( s, 8, 0xff );/* End marker */
2014 p_block->i_buffer = bs_pos( s ) / 8;
2015 p_block->i_pts = p_block->i_dts = p_subpic->i_start;
2016 if( !p_subpic->b_ephemer && ( p_subpic->i_stop > p_subpic->i_start ) )
2018 block_t *p_block_stop;
2020 p_block->i_length = p_subpic->i_stop - p_subpic->i_start;
2022 /* Send another (empty) subtitle to signal the end of display */
2023 p_block_stop = block_Alloc( 64000 );
2024 bs_init( s, p_block_stop->p_buffer, p_block_stop->i_buffer );
2025 bs_write( s, 8, 0x20 ); /* Data identifier */
2026 bs_write( s, 8, 0x0 ); /* Subtitle stream id */
2027 encode_page_composition( p_enc, s, 0 );
2028 bs_write( s, 8, 0x0f ); /* Sync byte */
2029 bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
2030 bs_write( s, 16, 1 ); /* Page id */
2031 bs_write( s, 16, 0 ); /* Segment length */
2032 bs_write( s, 8, 0xff );/* End marker */
2033 p_block_stop->i_buffer = bs_pos( s ) / 8;
2034 p_block_stop->i_pts = p_block_stop->i_dts = p_subpic->i_stop;
2035 block_ChainAppend( &p_block, p_block_stop );
2036 p_block_stop->i_length = VLC_TICK_FROM_MS(100); /* p_subpic->i_stop - p_subpic->i_start; */
2038 #ifdef DEBUG_DVBSUB
2039 msg_Dbg( p_enc, "subpicture encoded properly" );
2040 #endif
2041 return p_block;
2044 /*****************************************************************************
2045 * CloseEncoder: encoder destruction
2046 *****************************************************************************/
2047 static void CloseEncoder( vlc_object_t *p_this )
2049 encoder_t *p_enc = (encoder_t *)p_this;
2050 encoder_sys_t *p_sys = p_enc->p_sys;
2052 var_Destroy( p_this , ENC_CFG_PREFIX "x" );
2053 var_Destroy( p_this , ENC_CFG_PREFIX "y" );
2054 var_Destroy( p_this , ENC_CFG_PREFIX "timeout" );
2056 if( p_sys->i_regions ) free( p_sys->p_regions );
2057 free( p_sys );
2060 static void encode_page_composition( encoder_t *p_enc, bs_t *s,
2061 subpicture_t *p_subpic )
2063 encoder_sys_t *p_sys = p_enc->p_sys;
2064 subpicture_region_t *p_region;
2065 bool b_mode_change = false;
2066 int i_regions, i_timeout;
2068 bs_write( s, 8, 0x0f ); /* Sync byte */
2069 bs_write( s, 8, DVBSUB_ST_PAGE_COMPOSITION ); /* Segment type */
2070 bs_write( s, 16, 1 ); /* Page id */
2072 for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
2073 p_region; p_region = p_region->p_next, i_regions++ )
2075 if( i_regions >= p_sys->i_regions )
2077 encoder_region_t region;
2078 region.i_width = region.i_height = 0;
2079 p_sys->p_regions = xrealloc( p_sys->p_regions,
2080 sizeof(encoder_region_t) * (p_sys->i_regions + 1) );
2081 p_sys->p_regions[p_sys->i_regions++] = region;
2084 if( ( p_sys->p_regions[i_regions].i_width <
2085 (int)p_region->fmt.i_visible_width ) ||
2086 ( p_sys->p_regions[i_regions].i_width >
2087 (int)p_region->fmt.i_visible_width ) )
2089 b_mode_change = true;
2090 msg_Dbg( p_enc, "region %i width change: %i -> %i",
2091 i_regions, p_sys->p_regions[i_regions].i_width,
2092 p_region->fmt.i_visible_width );
2093 p_sys->p_regions[i_regions].i_width =
2094 p_region->fmt.i_visible_width;
2096 if( p_sys->p_regions[i_regions].i_height <
2097 (int)p_region->fmt.i_visible_height )
2099 b_mode_change = true;
2100 msg_Dbg( p_enc, "region %i height change: %i -> %i",
2101 i_regions, p_sys->p_regions[i_regions].i_height,
2102 p_region->fmt.i_visible_height );
2103 p_sys->p_regions[i_regions].i_height =
2104 p_region->fmt.i_visible_height;
2108 bs_write( s, 16, i_regions * 6 + 2 ); /* Segment length */
2110 i_timeout = 0;
2111 if( p_subpic && !p_subpic->b_ephemer &&
2112 ( p_subpic->i_stop > p_subpic->i_start ) )
2114 i_timeout = SEC_FROM_VLC_TICK(p_subpic->i_stop - p_subpic->i_start);
2117 bs_write( s, 8, i_timeout ); /* Timeout */
2118 bs_write( s, 4, p_sys->i_page_ver++ );
2119 bs_write( s, 2, b_mode_change ?
2120 DVBSUB_PCS_STATE_CHANGE : DVBSUB_PCS_STATE_ACQUISITION );
2121 bs_write( s, 2, 0 ); /* Reserved */
2123 for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
2124 p_region; p_region = p_region->p_next, i_regions++ )
2126 bs_write( s, 8, i_regions );
2127 bs_write( s, 8, 0 ); /* Reserved */
2128 if( (p_sys->i_offset_x > 0) && (p_sys->i_offset_y > 0) )
2130 bs_write( s, 16, p_sys->i_offset_x ); /* override x position */
2131 bs_write( s, 16, p_sys->i_offset_y ); /* override y position */
2133 else
2135 bs_write( s, 16, p_region->i_x );
2136 bs_write( s, 16, p_region->i_y );
2141 static void encode_clut( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
2143 encoder_sys_t *p_sys = p_enc->p_sys;
2144 subpicture_region_t *p_region = p_subpic->p_region;
2145 video_palette_t *p_pal, pal;
2147 /* Sanity check */
2148 if( !p_region ) return;
2150 if( p_region->fmt.i_chroma == VLC_CODEC_YUVP )
2152 p_pal = p_region->fmt.p_palette;
2154 else
2156 pal.i_entries = 4;
2157 for( int i = 0; i < 4; i++ )
2159 pal.palette[i][0] = 0;
2160 pal.palette[i][1] = 0;
2161 pal.palette[i][2] = 0;
2162 pal.palette[i][3] = 0;
2164 p_pal = &pal;
2167 bs_write( s, 8, 0x0f ); /* Sync byte */
2168 bs_write( s, 8, DVBSUB_ST_CLUT_DEFINITION ); /* Segment type */
2169 bs_write( s, 16, 1 ); /* Page id */
2171 bs_write( s, 16, p_pal->i_entries * 6 + 2 ); /* Segment length */
2172 bs_write( s, 8, 1 ); /* Clut id */
2173 bs_write( s, 4, p_sys->i_clut_ver++ );
2174 bs_write( s, 4, 0 ); /* Reserved */
2176 for( int i = 0; i < p_pal->i_entries; i++ )
2178 bs_write( s, 8, i ); /* Clut entry id */
2179 bs_write( s, 1, p_pal->i_entries == 4 ); /* 2bit/entry flag */
2180 bs_write( s, 1, p_pal->i_entries == 16 ); /* 4bit/entry flag */
2181 bs_write( s, 1, p_pal->i_entries == 256 ); /* 8bit/entry flag */
2182 bs_write( s, 4, 0 ); /* Reserved */
2183 bs_write( s, 1, 1 ); /* Full range flag */
2184 bs_write( s, 8, p_pal->palette[i][3] ? /* Y value */
2185 (p_pal->palette[i][0] ? p_pal->palette[i][0] : 16) : 0 );
2186 bs_write( s, 8, p_pal->palette[i][1] ); /* Cr value */
2187 bs_write( s, 8, p_pal->palette[i][2] ); /* Cb value */
2188 bs_write( s, 8, 0xff - p_pal->palette[i][3] ); /* T value */
2192 static void encode_region_composition( encoder_t *p_enc, bs_t *s,
2193 subpicture_t *p_subpic )
2195 encoder_sys_t *p_sys = p_enc->p_sys;
2196 subpicture_region_t *p_region;
2197 int i_region;
2199 for( i_region = 0, p_region = p_subpic->p_region; p_region;
2200 p_region = p_region->p_next, i_region++ )
2202 int i_entries = 4, i_depth = 0x1, i_bg = 0;
2203 bool b_text =
2204 ( p_region->fmt.i_chroma == VLC_CODEC_TEXT );
2206 if( !b_text )
2208 video_palette_t *p_pal = p_region->fmt.p_palette;
2210 if( !p_pal )
2212 msg_Err( p_enc, "subpicture has no palette - ignoring it" );
2213 break;
2216 i_entries = p_pal->i_entries;
2217 i_depth = i_entries == 4 ? 0x1 : i_entries == 16 ? 0x2 : 0x3;
2219 for( i_bg = 0; i_bg < p_pal->i_entries; i_bg++ )
2221 if( !p_pal->palette[i_bg][3] ) break;
2225 bs_write( s, 8, 0x0f ); /* Sync byte */
2226 bs_write( s, 8, DVBSUB_ST_REGION_COMPOSITION ); /* Segment type */
2227 bs_write( s, 16, 1 ); /* Page id */
2229 bs_write( s, 16, 10 + 6 + (b_text ? 2 : 0) ); /* Segment length */
2230 bs_write( s, 8, i_region );
2231 bs_write( s, 4, p_sys->i_region_ver++ );
2233 /* Region attributes */
2234 bs_write( s, 1, i_bg < i_entries ); /* Fill */
2235 bs_write( s, 3, 0 ); /* Reserved */
2236 bs_write( s, 16, p_sys->p_regions[i_region].i_width );
2237 bs_write( s, 16, p_sys->p_regions[i_region].i_height );
2238 bs_write( s, 3, i_depth ); /* Region level of compatibility */
2239 bs_write( s, 3, i_depth ); /* Region depth */
2240 bs_write( s, 2, 0 ); /* Reserved */
2241 bs_write( s, 8, 1 ); /* Clut id */
2242 bs_write( s, 8, i_bg ); /* region 8bit pixel code */
2243 bs_write( s, 4, i_bg ); /* region 4bit pixel code */
2244 bs_write( s, 2, i_bg ); /* region 2bit pixel code */
2245 bs_write( s, 2, 0 ); /* Reserved */
2247 /* In our implementation we only have 1 object per region */
2248 bs_write( s, 16, i_region );
2249 bs_write( s, 2, b_text ? DVBSUB_OT_BASIC_CHAR:DVBSUB_OT_BASIC_BITMAP );
2250 bs_write( s, 2, 0 ); /* object provider flag */
2251 bs_write( s, 12, 0 );/* object horizontal position */
2252 bs_write( s, 4, 0 ); /* Reserved */
2253 bs_write( s, 12, 0 );/* object vertical position */
2255 if( b_text )
2257 bs_write( s, 8, 1 ); /* foreground pixel code */
2258 bs_write( s, 8, 0 ); /* background pixel code */
2263 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
2264 subpicture_region_t *p_region,
2265 bool b_top );
2267 static void encode_object( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
2269 encoder_sys_t *p_sys = p_enc->p_sys;
2270 subpicture_region_t *p_region;
2271 int i_region;
2273 int i_length_pos, i_update_pos, i_pixel_data_pos;
2275 for( i_region = 0, p_region = p_subpic->p_region; p_region;
2276 p_region = p_region->p_next, i_region++ )
2278 bs_write( s, 8, 0x0f ); /* Sync byte */
2279 bs_write( s, 8, DVBSUB_ST_OBJECT_DATA ); /* Segment type */
2280 bs_write( s, 16, 1 ); /* Page id */
2282 i_length_pos = bs_pos( s );
2283 bs_write( s, 16, 0 ); /* Segment length */
2284 bs_write( s, 16, i_region ); /* Object id */
2285 bs_write( s, 4, p_sys->i_region_ver++ );
2287 /* object coding method */
2288 switch( p_region->fmt.i_chroma )
2290 case VLC_CODEC_YUVP:
2291 bs_write( s, 2, 0 );
2292 break;
2293 case VLC_CODEC_TEXT:
2294 bs_write( s, 2, 1 );
2295 break;
2296 default:
2297 msg_Err( p_enc, "FOURCC %d not supported by encoder.", p_region->fmt.i_chroma );
2298 continue;
2301 bs_write( s, 1, 0 ); /* non modifying color flag */
2302 bs_write( s, 1, 0 ); /* Reserved */
2304 if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
2306 int i_size, i;
2308 if( !p_region->p_text ) continue;
2310 i_size = __MIN( strlen( p_region->p_text->psz_text ), 256 );
2312 bs_write( s, 8, i_size ); /* number of characters in string */
2313 for( i = 0; i < i_size; i++ )
2315 bs_write( s, 16, p_region->p_text->psz_text[i] );
2318 /* Update segment length */
2319 SetWBE( &s->p_start[i_length_pos/8],
2320 (bs_pos(s) - i_length_pos)/8 -2 );
2321 continue;
2324 /* Coding of a bitmap object */
2325 i_update_pos = bs_pos( s );
2326 bs_write( s, 16, 0 ); /* topfield data block length */
2327 bs_write( s, 16, 0 ); /* bottomfield data block length */
2329 /* Top field */
2330 i_pixel_data_pos = bs_pos( s );
2331 encode_pixel_data( p_enc, s, p_region, true );
2332 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
2333 SetWBE( &s->p_start[i_update_pos/8], i_pixel_data_pos );
2335 /* Bottom field */
2336 i_pixel_data_pos = bs_pos( s );
2337 encode_pixel_data( p_enc, s, p_region, false );
2338 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
2339 SetWBE( &s->p_start[i_update_pos/8+2], i_pixel_data_pos );
2341 /* Stuffing for word alignment */
2342 bs_align_0( s );
2343 if( bs_pos( s ) % 16 ) bs_write( s, 8, 0 );
2345 /* Update segment length */
2346 SetWBE( &s->p_start[i_length_pos/8], (bs_pos(s) - i_length_pos)/8 -2 );
2350 static void encode_pixel_line_2bp( bs_t *s, subpicture_region_t *p_region,
2351 int i_line );
2352 static void encode_pixel_line_4bp( bs_t *s, subpicture_region_t *p_region,
2353 int i_line );
2354 static void encode_pixel_line_8bp( bs_t *s, subpicture_region_t *p_region,
2355 int i_line );
2356 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
2357 subpicture_region_t *p_region,
2358 bool b_top )
2360 unsigned int i_line;
2362 /* Sanity check */
2363 if( p_region->fmt.i_chroma != VLC_CODEC_YUVP ) return;
2365 /* Encode line by line */
2366 for( i_line = !b_top; i_line < p_region->fmt.i_visible_height;
2367 i_line += 2 )
2369 switch( p_region->fmt.p_palette->i_entries )
2371 case 0:
2372 break;
2374 case 4:
2375 bs_write( s, 8, 0x10 ); /* 2 bit/pixel code string */
2376 encode_pixel_line_2bp( s, p_region, i_line );
2377 break;
2379 case 16:
2380 bs_write( s, 8, 0x11 ); /* 4 bit/pixel code string */
2381 encode_pixel_line_4bp( s, p_region, i_line );
2382 break;
2384 case 256:
2385 bs_write( s, 8, 0x12 ); /* 8 bit/pixel code string */
2386 encode_pixel_line_8bp( s, p_region, i_line );
2387 break;
2389 default:
2390 msg_Err( p_enc, "subpicture palette (%i) not handled",
2391 p_region->fmt.p_palette->i_entries );
2392 break;
2395 bs_write( s, 8, 0xf0 ); /* End of object line code */
2399 static void encode_pixel_line_2bp( bs_t *s, subpicture_region_t *p_region,
2400 int i_line )
2402 unsigned int i, i_length = 0;
2403 int i_pitch = p_region->p_picture->p->i_pitch;
2404 uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
2405 int i_last_pixel = p_data[0];
2407 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2409 if( ( i != p_region->fmt.i_visible_width ) &&
2410 ( p_data[i] == i_last_pixel ) && ( i_length != 284 ) )
2412 i_length++;
2413 continue;
2416 if( ( i_length == 1 ) || ( i_length == 11 ) || ( i_length == 28 ) )
2418 /* 2bit/pixel code */
2419 if( i_last_pixel )
2420 bs_write( s, 2, i_last_pixel );
2421 else
2423 bs_write( s, 2, 0 );
2424 bs_write( s, 1, 0 );
2425 bs_write( s, 1, 1 ); /* pseudo color 0 */
2427 i_length--;
2430 if( i_length == 2 )
2432 if( i_last_pixel )
2434 bs_write( s, 2, i_last_pixel );
2435 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, 0 );
2442 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
2445 else if( i_length > 2 )
2447 bs_write( s, 2, 0 );
2448 if( i_length <= 10 )
2450 bs_write( s, 1, 1 );
2451 bs_write( s, 3, i_length - 3 );
2452 bs_write( s, 2, i_last_pixel );
2454 else
2456 bs_write( s, 1, 0 );
2457 bs_write( s, 1, 0 );
2459 if( i_length <= 27 )
2461 bs_write( s, 2, 2 );
2462 bs_write( s, 4, i_length - 12 );
2463 bs_write( s, 2, i_last_pixel );
2465 else
2467 bs_write( s, 2, 3 );
2468 bs_write( s, 8, i_length - 29 );
2469 bs_write( s, 2, i_last_pixel );
2474 if( i == p_region->fmt.i_visible_width ) break;
2476 i_last_pixel = p_data[i];
2477 i_length = 1;
2480 /* Stop */
2481 bs_write( s, 2, 0 );
2482 bs_write( s, 1, 0 );
2483 bs_write( s, 1, 0 );
2484 bs_write( s, 2, 0 );
2486 /* Stuffing */
2487 bs_align_0( s );
2490 static void encode_pixel_line_4bp( bs_t *s, subpicture_region_t *p_region,
2491 int i_line )
2493 unsigned int i, i_length = 0;
2494 int i_pitch = p_region->p_picture->p->i_pitch;
2495 uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
2496 int i_last_pixel = p_data[0];
2498 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2500 if( i != p_region->fmt.i_visible_width &&
2501 p_data[i] == i_last_pixel && i_length != 280 )
2503 i_length++;
2504 continue;
2507 if( ( i_length == 1 ) ||
2508 ( ( i_length == 3 ) && i_last_pixel ) ||
2509 ( i_length == 8 ) )
2511 /* 4bit/pixel code */
2512 if( i_last_pixel )
2513 bs_write( s, 4, i_last_pixel );
2514 else
2516 bs_write( s, 4, 0 );
2517 bs_write( s, 1, 1 );
2518 bs_write( s, 1, 1 );
2519 bs_write( s, 2, 0 ); /* pseudo color 0 */
2521 i_length--;
2524 if( i_length == 2 )
2526 if( i_last_pixel )
2528 bs_write( s, 4, i_last_pixel );
2529 bs_write( s, 4, i_last_pixel );
2531 else
2533 bs_write( s, 4, 0 );
2534 bs_write( s, 1, 1 );
2535 bs_write( s, 1, 1 );
2536 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
2539 else if( !i_last_pixel && ( i_length >= 3 ) && ( i_length <= 9 ) )
2541 bs_write( s, 4, 0 );
2542 bs_write( s, 1, 0 );
2543 bs_write( s, 3, i_length - 2 ); /* (i_length - 2) * color 0 */
2545 else if( i_length > 2 )
2547 bs_write( s, 4, 0 );
2548 bs_write( s, 1, 1 );
2550 if( i_length <= 7 )
2552 bs_write( s, 1, 0 );
2553 bs_write( s, 2, i_length - 4 );
2554 bs_write( s, 4, i_last_pixel );
2556 else
2558 bs_write( s, 1, 1 );
2560 if( i_length <= 24 )
2562 bs_write( s, 2, 2 );
2563 bs_write( s, 4, i_length - 9 );
2564 bs_write( s, 4, i_last_pixel );
2566 else
2568 bs_write( s, 2, 3 );
2569 bs_write( s, 8, i_length - 25 );
2570 bs_write( s, 4, i_last_pixel );
2575 if( i == p_region->fmt.i_visible_width ) break;
2577 i_last_pixel = p_data[i];
2578 i_length = 1;
2581 /* Stop */
2582 bs_write( s, 8, 0 );
2584 /* Stuffing */
2585 bs_align_0( s );
2588 static void encode_pixel_line_8bp( bs_t *s, subpicture_region_t *p_region,
2589 int i_line )
2591 unsigned int i, i_length = 0;
2592 int i_pitch = p_region->p_picture->p->i_pitch;
2593 uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
2594 int i_last_pixel = p_data[0];
2596 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2598 if( ( i != p_region->fmt.i_visible_width ) &&
2599 ( p_data[i] == i_last_pixel ) && ( i_length != 127 ) )
2601 i_length++;
2602 continue;
2605 if( ( i_length == 1 ) && i_last_pixel )
2607 /* 8bit/pixel code */
2608 bs_write( s, 8, i_last_pixel );
2610 else if( ( i_length == 2 ) && i_last_pixel )
2612 /* 8bit/pixel code */
2613 bs_write( s, 8, i_last_pixel );
2614 bs_write( s, 8, i_last_pixel );
2616 else if( i_length <= 127 )
2618 bs_write( s, 8, 0 );
2620 if( !i_last_pixel )
2622 bs_write( s, 1, 0 );
2623 bs_write( s, 7, i_length ); /* pseudo color 0 */
2625 else
2627 bs_write( s, 1, 1 );
2628 bs_write( s, 7, i_length );
2629 bs_write( s, 8, i_last_pixel );
2633 if( i == p_region->fmt.i_visible_width ) break;
2635 i_last_pixel = p_data[i];
2636 i_length = 1;
2639 /* Stop */
2640 bs_write( s, 8, 0 );
2641 bs_write( s, 8, 0 );
2643 /* Stuffing */
2644 bs_align_0( s );
2647 #endif
2649 static void default_dds_init( decoder_t * p_dec )
2651 decoder_sys_t *p_sys = p_dec->p_sys;
2653 /* see notes on DDS at the top of the file */
2655 /* configure for SD res in case DDS is not present */
2656 p_sys->display.i_version = 0xff; /* an invalid version so it's always different */
2657 p_sys->display.i_width = 720;
2658 p_sys->display.i_height = 576;
2659 p_sys->display.b_windowed = false;