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-2005 the VideoLAN team
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>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28 *****************************************************************************/
29 /*****************************************************************************
33 * DVB subtitles coded as strings of characters are not handled correctly.
34 * The character codes in the string should actually be indexes referring to a
35 * character table identified in the subtitle descriptor.
37 * The spec is quite vague in this area, but what is meant is perhaps that it
38 * refers to the character index in the codepage belonging to the language specified
39 * in the subtitle descriptor. Potentially it's designed for widechar
40 * (but not for UTF-*) codepages.
41 *****************************************************************************/
46 #include <vlc_common.h>
47 #include <vlc_plugin.h>
49 #include <vlc_codec.h>
54 /* #define DEBUG_DVBSUB 1 */
56 #define POSX_TEXT N_("Decoding X coordinate")
57 #define POSX_LONGTEXT N_("X coordinate of the rendered subtitle")
59 #define POSY_TEXT N_("Decoding Y coordinate")
60 #define POSY_LONGTEXT N_("Y coordinate of the rendered subtitle")
62 #define POS_TEXT N_("Subpicture position")
63 #define POS_LONGTEXT N_( \
64 "You can enforce the subpicture position on the video " \
65 "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
66 "also use combinations of these values, e.g. 6=top-right).")
68 #define ENC_POSX_TEXT N_("Encoding X coordinate")
69 #define ENC_POSX_LONGTEXT N_("X coordinate of the encoded subtitle" )
70 #define ENC_POSY_TEXT N_("Encoding Y coordinate")
71 #define ENC_POSY_LONGTEXT N_("Y coordinate of the encoded subtitle" )
73 static const int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
74 static const char *const ppsz_pos_descriptions[] =
75 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
76 N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
78 /*****************************************************************************
80 *****************************************************************************/
81 static int Open ( vlc_object_t * );
82 static void Close( vlc_object_t * );
83 static subpicture_t *Decode( decoder_t *, block_t ** );
85 static int OpenEncoder ( vlc_object_t * );
86 static void CloseEncoder( vlc_object_t * );
87 static block_t *Encode ( encoder_t *, subpicture_t * );
90 # define DVBSUB_CFG_PREFIX "dvbsub-"
91 set_description( N_("DVB subtitles decoder") )
92 set_capability( "decoder", 50 )
93 set_category( CAT_INPUT )
94 set_subcategory( SUBCAT_INPUT_SCODEC )
95 set_callbacks( Open, Close )
97 add_integer( DVBSUB_CFG_PREFIX "position", 8, NULL, POS_TEXT, POS_LONGTEXT, true )
98 change_integer_list( pi_pos_values, ppsz_pos_descriptions, NULL );
99 add_integer( DVBSUB_CFG_PREFIX "x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, false )
100 add_integer( DVBSUB_CFG_PREFIX "y", -1, NULL, POSY_TEXT, POSY_LONGTEXT, false )
102 # define ENC_CFG_PREFIX "sout-dvbsub-"
104 set_description( N_("DVB subtitles encoder") )
105 set_capability( "encoder", 100 )
106 set_callbacks( OpenEncoder, CloseEncoder )
108 add_integer( ENC_CFG_PREFIX "x", -1, NULL, ENC_POSX_TEXT, ENC_POSX_LONGTEXT, false )
109 add_integer( ENC_CFG_PREFIX "y", -1, NULL, ENC_POSY_TEXT, ENC_POSY_LONGTEXT, false )
110 add_obsolete_integer( ENC_CFG_PREFIX "timeout" ) /* Suppressed since 0.8.5 */
113 static const char *const ppsz_enc_options[] = { "x", "y", NULL };
115 /****************************************************************************
117 ****************************************************************************
118 * Those structures refer closely to the ETSI 300 743 Object model
119 ****************************************************************************/
121 /* The object definition gives the position of the object in a region [7.2.5] */
122 typedef struct dvbsub_objectdef_s
130 char *psz_text; /* for string of characters objects */
132 } dvbsub_objectdef_t;
134 /* The entry in the palette CLUT */
144 /* The displays dimensions [7.2.1] */
145 typedef struct dvbsub_display_s
162 typedef struct dvbsub_clut_s
166 dvbsub_color_t c_2b[4];
167 dvbsub_color_t c_4b[16];
168 dvbsub_color_t c_8b[256];
170 struct dvbsub_clut_s *p_next;
174 /* The Region is an aera on the image [7.2.3]
175 * with a list of the object definitions associated and a CLUT */
176 typedef struct dvbsub_region_s
191 dvbsub_objectdef_t *p_object_defs;
193 struct dvbsub_region_s *p_next;
197 /* The object definition gives the position of the object in a region */
198 typedef struct dvbsub_regiondef_s
204 } dvbsub_regiondef_t;
206 /* The page defines the list of regions [7.2.2] */
210 int i_timeout; /* in seconds */
215 dvbsub_regiondef_t *p_region_defs;
223 /* Decoder internal data */
234 dvbsub_page_t *p_page;
235 dvbsub_region_t *p_regions;
236 dvbsub_clut_t *p_cluts;
237 dvbsub_display_t *p_display;
238 dvbsub_clut_t default_clut;
242 /* List of different SEGMENT TYPES */
243 /* According to EN 300-743, table 2 */
244 #define DVBSUB_ST_PAGE_COMPOSITION 0x10
245 #define DVBSUB_ST_REGION_COMPOSITION 0x11
246 #define DVBSUB_ST_CLUT_DEFINITION 0x12
247 #define DVBSUB_ST_OBJECT_DATA 0x13
248 #define DVBSUB_ST_DISPLAY_DEFINITION 0x14
249 #define DVBSUB_ST_ENDOFDISPLAY 0x80
250 #define DVBSUB_ST_STUFFING 0xff
251 /* List of different OBJECT TYPES */
252 /* According to EN 300-743, table 6 */
253 #define DVBSUB_OT_BASIC_BITMAP 0x00
254 #define DVBSUB_OT_BASIC_CHAR 0x01
255 #define DVBSUB_OT_COMPOSITE_STRING 0x02
256 /* Pixel DATA TYPES */
257 /* According to EN 300-743, table 9 */
258 #define DVBSUB_DT_2BP_CODE_STRING 0x10
259 #define DVBSUB_DT_4BP_CODE_STRING 0x11
260 #define DVBSUB_DT_8BP_CODE_STRING 0x12
261 #define DVBSUB_DT_24_TABLE_DATA 0x20
262 #define DVBSUB_DT_28_TABLE_DATA 0x21
263 #define DVBSUB_DT_48_TABLE_DATA 0x22
264 #define DVBSUB_DT_END_LINE 0xf0
265 /* List of different Page Composition Segment state */
266 /* According to EN 300-743, 7.2.1 table 3 */
267 #define DVBSUB_PCS_STATE_ACQUISITION 0x01
268 #define DVBSUB_PCS_STATE_CHANGE 0x02
270 /*****************************************************************************
272 *****************************************************************************/
273 static void decode_segment( decoder_t *, bs_t * );
274 static void decode_page_composition( decoder_t *, bs_t * );
275 static void decode_region_composition( decoder_t *, bs_t * );
276 static void decode_object( decoder_t *, bs_t * );
277 static void decode_display_definition( decoder_t *, bs_t * );
278 static void decode_clut( decoder_t *, bs_t * );
279 static void free_all( decoder_t * );
281 static void default_clut_init( decoder_t * );
283 static subpicture_t *render( decoder_t * );
285 /*****************************************************************************
286 * Open: probe the decoder and return score
287 *****************************************************************************
288 * Tries to launch a decoder and return score so that the interface is able
290 *****************************************************************************/
291 static int Open( vlc_object_t *p_this )
293 decoder_t *p_dec = (decoder_t *) p_this;
294 decoder_sys_t *p_sys;
298 if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','v','b','s') )
303 p_dec->pf_decode_sub = Decode;
304 p_sys = p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
307 memset( p_sys, 0, sizeof(decoder_sys_t) );
309 p_sys->i_pts = (mtime_t) 0;
310 p_sys->i_id = p_dec->fmt_in.subs.dvb.i_id & 0xFFFF;
311 p_sys->i_ancillary_id = p_dec->fmt_in.subs.dvb.i_id >> 16;
313 p_sys->p_regions = NULL;
314 p_sys->p_cluts = NULL;
315 p_sys->p_page = NULL;
316 p_sys->p_display = NULL;
318 var_Create( p_this, DVBSUB_CFG_PREFIX "position",
319 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
320 var_Get( p_this, DVBSUB_CFG_PREFIX "position", &val );
321 p_sys->i_spu_position = val.i_int;
322 var_Create( p_this, DVBSUB_CFG_PREFIX "x",
323 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
324 var_Get( p_this, DVBSUB_CFG_PREFIX "x", &val );
326 var_Create( p_this, DVBSUB_CFG_PREFIX "y",
327 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
328 var_Get( p_this, DVBSUB_CFG_PREFIX "y", &val );
331 /* Check if subpicture position was overridden */
332 p_sys->b_absolute = true;
333 p_sys->i_spu_x = p_sys->i_spu_y = 0;
335 if( ( i_posx >= 0 ) && ( i_posy >= 0 ) )
337 p_sys->b_absolute = true;
338 p_sys->i_spu_x = i_posx;
339 p_sys->i_spu_y = i_posy;
342 es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'd','v','b','s' ) );
344 default_clut_init( p_dec );
349 /*****************************************************************************
351 *****************************************************************************/
352 static void Close( vlc_object_t *p_this )
354 decoder_t *p_dec = (decoder_t*) p_this;
355 decoder_sys_t *p_sys = p_dec->p_sys;
357 var_Destroy( p_this, DVBSUB_CFG_PREFIX "x" );
358 var_Destroy( p_this, DVBSUB_CFG_PREFIX "y" );
359 var_Destroy( p_this, DVBSUB_CFG_PREFIX "position" );
365 /*****************************************************************************
367 *****************************************************************************/
368 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
370 decoder_sys_t *p_sys = p_dec->p_sys;
372 subpicture_t *p_spu = NULL;
374 if( ( pp_block == NULL ) || ( *pp_block == NULL ) ) return NULL;
378 p_sys->i_pts = p_block->i_pts;
379 if( p_sys->i_pts <= 0 )
382 /* Some DVB channels send stuffing segments in non-dated packets so
383 * don't complain too loudly. */
384 msg_Warn( p_dec, "non dated subtitle" );
386 block_Release( p_block );
390 bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
392 if( bs_read( &p_sys->bs, 8 ) != 0x20 ) /* Data identifier */
394 msg_Dbg( p_dec, "invalid data identifier" );
395 block_Release( p_block );
399 if( bs_read( &p_sys->bs, 8 ) ) /* Subtitle stream id */
401 msg_Dbg( p_dec, "invalid subtitle stream id" );
402 block_Release( p_block );
407 msg_Dbg( p_dec, "subtitle packet received: %"PRId64, p_sys->i_pts );
410 p_sys->b_page = false;
411 while( bs_show( &p_sys->bs, 8 ) == 0x0f ) /* Sync byte */
413 decode_segment( p_dec, &p_sys->bs );
416 if( bs_read( &p_sys->bs, 8 ) != 0xff ) /* End marker */
418 msg_Warn( p_dec, "end marker not found (corrupted subtitle ?)" );
419 block_Release( p_block );
423 /* Check if the page is to be displayed */
424 if( p_sys->p_page && p_sys->b_page )
425 p_spu = render( p_dec );
427 block_Release( p_block );
432 /* following functions are local */
434 /*****************************************************************************
435 * default_clut_init: default clut as defined in EN 300-743 section 10
436 *****************************************************************************/
437 static void default_clut_init( decoder_t *p_dec )
439 decoder_sys_t *p_sys = p_dec->p_sys;
442 #define RGB_TO_Y(r, g, b) ((int16_t) 77 * r + 150 * g + 29 * b) / 256;
443 #define RGB_TO_U(r, g, b) ((int16_t) -44 * r - 87 * g + 131 * b) / 256;
444 #define RGB_TO_V(r, g, b) ((int16_t) 131 * r - 110 * g - 21 * b) / 256;
447 for( i = 0; i < 4; i++ )
449 uint8_t R = 0, G = 0, B = 0, T = 0;
451 if( !(i & 0x2) && !(i & 0x1) ) T = 0xFF;
452 else if( !(i & 0x2) && (i & 0x1) ) R = G = B = 0xFF;
453 else if( (i & 0x2) && !(i & 0x1) ) R = G = B = 0;
454 else R = G = B = 0x7F;
456 p_sys->default_clut.c_2b[i].Y = RGB_TO_Y(R,G,B);
457 p_sys->default_clut.c_2b[i].Cb = RGB_TO_V(R,G,B);
458 p_sys->default_clut.c_2b[i].Cr = RGB_TO_U(R,G,B);
459 p_sys->default_clut.c_2b[i].T = T;
462 /* 16 entries CLUT */
463 for( i = 0; i < 16; i++ )
465 uint8_t R = 0, G = 0, B = 0, T = 0;
469 if( !(i & 0x4) && !(i & 0x2) && !(i & 0x1) )
475 R = (i & 0x1) ? 0xFF : 0;
476 G = (i & 0x2) ? 0xFF : 0;
477 B = (i & 0x4) ? 0xFF : 0;
482 R = (i & 0x1) ? 0x7F : 0;
483 G = (i & 0x2) ? 0x7F : 0;
484 B = (i & 0x4) ? 0x7F : 0;
487 p_sys->default_clut.c_4b[i].Y = RGB_TO_Y(R,G,B);
488 p_sys->default_clut.c_4b[i].Cr = RGB_TO_V(R,G,B);
489 p_sys->default_clut.c_4b[i].Cb = RGB_TO_U(R,G,B);
490 p_sys->default_clut.c_4b[i].T = T;
493 /* 256 entries CLUT */
494 memset( p_sys->default_clut.c_8b, 0xFF, 256 * sizeof(dvbsub_color_t) );
497 static void decode_segment( decoder_t *p_dec, bs_t *s )
499 decoder_sys_t *p_sys = p_dec->p_sys;
504 /* sync_byte (already checked) */
508 i_type = bs_read( s, 8 );
511 i_page_id = bs_read( s, 16 );
514 i_size = bs_show( s, 16 );
516 if( ( i_page_id != p_sys->i_id ) &&
517 ( i_page_id != p_sys->i_ancillary_id ) )
520 msg_Dbg( p_dec, "subtitle skipped (page id: %i, %i)",
521 i_page_id, p_sys->i_id );
523 bs_skip( s, 8 * ( 2 + i_size ) );
527 if( ( p_sys->i_ancillary_id != p_sys->i_id ) &&
528 ( i_type == DVBSUB_ST_PAGE_COMPOSITION ) &&
529 ( i_page_id == p_sys->i_ancillary_id ) )
532 msg_Dbg( p_dec, "skipped invalid ancillary subtitle packet" );
534 bs_skip( s, 8 * ( 2 + i_size ) );
539 if( i_page_id == p_sys->i_id )
540 msg_Dbg( p_dec, "segment (id: %i)", i_page_id );
542 msg_Dbg( p_dec, "ancillary segment (id: %i)", i_page_id );
547 case DVBSUB_ST_PAGE_COMPOSITION:
549 msg_Dbg( p_dec, "decode_page_composition" );
551 decode_page_composition( p_dec, s );
554 case DVBSUB_ST_REGION_COMPOSITION:
556 msg_Dbg( p_dec, "decode_region_composition" );
558 decode_region_composition( p_dec, s );
561 case DVBSUB_ST_CLUT_DEFINITION:
563 msg_Dbg( p_dec, "decode_clut" );
565 decode_clut( p_dec, s );
568 case DVBSUB_ST_OBJECT_DATA:
570 msg_Dbg( p_dec, "decode_object" );
572 decode_object( p_dec, s );
575 case DVBSUB_ST_DISPLAY_DEFINITION:
577 msg_Dbg( p_dec, "decode_display_definition" );
579 decode_display_definition( p_dec, s );
582 case DVBSUB_ST_ENDOFDISPLAY:
584 msg_Dbg( p_dec, "end of display" );
586 bs_skip( s, 8 * ( 2 + i_size ) );
589 case DVBSUB_ST_STUFFING:
591 msg_Dbg( p_dec, "skip stuffing" );
593 bs_skip( s, 8 * ( 2 + i_size ) );
597 msg_Warn( p_dec, "unsupported segment type: (%04x)", i_type );
598 bs_skip( s, 8 * ( 2 + i_size ) );
603 static void decode_clut( decoder_t *p_dec, bs_t *s )
605 decoder_sys_t *p_sys = p_dec->p_sys;
606 uint16_t i_segment_length;
607 uint16_t i_processed_length;
608 dvbsub_clut_t *p_clut, *p_next;
611 i_segment_length = bs_read( s, 16 );
612 i_id = bs_read( s, 8 );
613 i_version = bs_read( s, 4 );
615 /* Check if we already have this clut */
616 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next )
618 if( p_clut->i_id == i_id ) break;
621 /* Check version number */
622 if( p_clut && ( p_clut->i_version == i_version ) )
625 bs_skip( s, 8 * i_segment_length - 12 );
632 msg_Dbg( p_dec, "new clut: %i", i_id );
634 p_clut = malloc( sizeof( dvbsub_clut_t ) );
637 p_clut->p_next = p_sys->p_cluts;
638 p_sys->p_cluts = p_clut;
641 /* Initialize to default clut */
642 p_next = p_clut->p_next;
643 *p_clut = p_sys->default_clut;
644 p_clut->p_next = p_next;
646 /* We don't have this version of the CLUT: Parse it */
647 p_clut->i_version = i_version;
649 bs_skip( s, 4 ); /* Reserved bits */
650 i_processed_length = 2;
651 while( i_processed_length < i_segment_length )
653 uint8_t y, cb, cr, t;
657 i_id = bs_read( s, 8 );
658 i_type = bs_read( s, 3 );
662 if( bs_read( s, 1 ) )
665 cr = bs_read( s, 8 );
666 cb = bs_read( s, 8 );
668 i_processed_length += 6;
672 y = bs_read( s, 6 ) << 2;
673 cr = bs_read( s, 4 ) << 4;
674 cb = bs_read( s, 4 ) << 4;
675 t = bs_read( s, 2 ) << 6;
676 i_processed_length += 4;
679 /* We are not entirely compliant here as full transparency is indicated
680 * with a luma value of zero, not a transparency value of 0xff
681 * (full transparency would actually be 0xff + 1). */
688 /* According to EN 300-743 section 7.2.3 note 1, type should
689 * not have more than 1 bit set to one, but some streams don't
690 * respect this note. */
691 if( ( i_type & 0x04 ) && ( i_id < 4 ) )
693 p_clut->c_2b[i_id].Y = y;
694 p_clut->c_2b[i_id].Cr = cr;
695 p_clut->c_2b[i_id].Cb = cb;
696 p_clut->c_2b[i_id].T = t;
698 if( ( i_type & 0x02 ) && ( i_id < 16 ) )
700 p_clut->c_4b[i_id].Y = y;
701 p_clut->c_4b[i_id].Cr = cr;
702 p_clut->c_4b[i_id].Cb = cb;
703 p_clut->c_4b[i_id].T = t;
707 p_clut->c_8b[i_id].Y = y;
708 p_clut->c_8b[i_id].Cr = cr;
709 p_clut->c_8b[i_id].Cb = cb;
710 p_clut->c_8b[i_id].T = t;
715 static void decode_page_composition( decoder_t *p_dec, bs_t *s )
717 decoder_sys_t *p_sys = p_dec->p_sys;
718 int i_version, i_state, i_segment_length, i_timeout, i;
720 /* A page is composed by 0 or more region */
721 i_segment_length = bs_read( s, 16 );
722 i_timeout = bs_read( s, 8 );
723 i_version = bs_read( s, 4 );
724 i_state = bs_read( s, 2 );
725 bs_skip( s, 2 ); /* Reserved */
727 if( i_state == DVBSUB_PCS_STATE_CHANGE )
729 /* End of an epoch, reset decoder buffer */
731 msg_Dbg( p_dec, "page composition mode change" );
735 else if( !p_sys->p_page && ( i_state != DVBSUB_PCS_STATE_ACQUISITION ) &&
736 ( i_state != DVBSUB_PCS_STATE_CHANGE ) )
738 /* Not a full PCS, we need to wait for one */
739 msg_Dbg( p_dec, "didn't receive an acquisition page yet" );
742 /* Try to start decoding even without an acquisition page */
743 bs_skip( s, 8 * (i_segment_length - 2) );
749 if( i_state == DVBSUB_PCS_STATE_ACQUISITION )
750 msg_Dbg( p_dec, "acquisition page composition" );
753 /* Check version number */
754 if( p_sys->p_page && ( p_sys->p_page->i_version == i_version ) )
756 bs_skip( s, 8 * (i_segment_length - 2) );
759 else if( p_sys->p_page )
761 if( p_sys->p_page->i_region_defs )
762 free( p_sys->p_page->p_region_defs );
763 p_sys->p_page->p_region_defs = NULL;
764 p_sys->p_page->i_region_defs = 0;
770 msg_Dbg( p_dec, "new page" );
772 /* Allocate a new page */
773 p_sys->p_page = malloc( sizeof(dvbsub_page_t) );
778 p_sys->p_page->i_version = i_version;
779 p_sys->p_page->i_timeout = i_timeout;
780 p_sys->b_page = true;
782 /* Number of regions */
783 p_sys->p_page->i_region_defs = (i_segment_length - 2) / 6;
785 if( p_sys->p_page->i_region_defs == 0 ) return;
787 p_sys->p_page->p_region_defs =
788 malloc( p_sys->p_page->i_region_defs * sizeof(dvbsub_region_t) );
789 if( p_sys->p_page->p_region_defs )
791 for( i = 0; i < p_sys->p_page->i_region_defs; i++ )
793 p_sys->p_page->p_region_defs[i].i_id = bs_read( s, 8 );
794 bs_skip( s, 8 ); /* Reserved */
795 p_sys->p_page->p_region_defs[i].i_x = bs_read( s, 16 );
796 p_sys->p_page->p_region_defs[i].i_y = bs_read( s, 16 );
799 msg_Dbg( p_dec, "page_composition, region %i (%i,%i)",
800 i, p_sys->p_page->p_region_defs[i].i_x,
801 p_sys->p_page->p_region_defs[i].i_y );
807 static void decode_region_composition( decoder_t *p_dec, bs_t *s )
809 decoder_sys_t *p_sys = p_dec->p_sys;
810 dvbsub_region_t *p_region, **pp_region = &p_sys->p_regions;
811 int i_segment_length, i_processed_length, i_id, i_version;
812 int i_width, i_height, i_level_comp, i_depth, i_clut;
813 int i_8_bg, i_4_bg, i_2_bg;
816 i_segment_length = bs_read( s, 16 );
817 i_id = bs_read( s, 8 );
818 i_version = bs_read( s, 4 );
820 /* Check if we already have this region */
821 for( p_region = p_sys->p_regions; p_region != NULL;
822 p_region = p_region->p_next )
824 pp_region = &p_region->p_next;
825 if( p_region->i_id == i_id ) break;
828 /* Check version number */
829 if( p_region && ( p_region->i_version == i_version ) )
831 bs_skip( s, 8 * (i_segment_length - 1) - 4 );
838 msg_Dbg( p_dec, "new region: %i", i_id );
840 p_region = *pp_region = malloc( sizeof(dvbsub_region_t) );
843 memset( p_region, 0, sizeof(dvbsub_region_t) );
844 p_region->p_object_defs = NULL;
845 p_region->p_pixbuf = NULL;
846 p_region->p_next = NULL;
849 /* Region attributes */
850 p_region->i_id = i_id;
851 p_region->i_version = i_version;
852 b_fill = bs_read( s, 1 );
853 bs_skip( s, 3 ); /* Reserved */
855 i_width = bs_read( s, 16 );
856 i_height = bs_read( s, 16 );
858 msg_Dbg( p_dec, " width=%d height=%d", i_width, i_height );
860 i_level_comp = bs_read( s, 3 );
861 i_depth = bs_read( s, 3 );
862 bs_skip( s, 2 ); /* Reserved */
863 i_clut = bs_read( s, 8 );
865 i_8_bg = bs_read( s, 8 );
866 i_4_bg = bs_read( s, 4 );
867 i_2_bg = bs_read( s, 2 );
868 bs_skip( s, 2 ); /* Reserved */
870 /* Free old object defs */
871 while( p_region->i_object_defs )
872 free( p_region->p_object_defs[--p_region->i_object_defs].psz_text );
874 free( p_region->p_object_defs );
875 p_region->p_object_defs = NULL;
877 /* Extra sanity checks */
878 if( ( p_region->i_width != i_width ) ||
879 ( p_region->i_height != i_height ) )
881 if( p_region->p_pixbuf )
883 msg_Dbg( p_dec, "region size changed (%dx%d->%dx%d)",
884 p_region->i_width, p_region->i_height, i_width, i_height );
885 free( p_region->p_pixbuf );
888 p_region->p_pixbuf = malloc( i_height * i_width );
889 p_region->i_depth = 0;
892 if( p_region->i_depth &&
893 ( ( p_region->i_depth != i_depth ) ||
894 ( p_region->i_level_comp != i_level_comp ) ||
895 ( p_region->i_clut != i_clut) ) )
897 msg_Dbg( p_dec, "region parameters changed (not allowed)" );
900 /* Erase background of region */
903 int i_background = ( p_region->i_depth == 1 ) ? i_2_bg :
904 ( ( p_region->i_depth == 2 ) ? i_4_bg : i_8_bg );
905 memset( p_region->p_pixbuf, i_background, i_width * i_height );
908 p_region->i_width = i_width;
909 p_region->i_height = i_height;
910 p_region->i_level_comp = i_level_comp;
911 p_region->i_depth = i_depth;
912 p_region->i_clut = i_clut;
914 /* List of objects in the region */
915 i_processed_length = 10;
916 while( i_processed_length < i_segment_length )
918 dvbsub_objectdef_t *p_obj;
920 /* We create a new object */
921 p_region->i_object_defs++;
922 p_region->p_object_defs =
923 realloc( p_region->p_object_defs,
924 sizeof(dvbsub_objectdef_t) * p_region->i_object_defs );
926 /* We parse object properties */
927 p_obj = &p_region->p_object_defs[p_region->i_object_defs - 1];
928 p_obj->i_id = bs_read( s, 16 );
929 p_obj->i_type = bs_read( s, 2 );
930 bs_skip( s, 2 ); /* Provider */
931 p_obj->i_x = bs_read( s, 12 );
932 bs_skip( s, 4 ); /* Reserved */
933 p_obj->i_y = bs_read( s, 12 );
934 p_obj->psz_text = NULL;
936 i_processed_length += 6;
938 if( ( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ) ||
939 ( p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING ) )
941 p_obj->i_fg_pc = bs_read( s, 8 );
942 p_obj->i_bg_pc = bs_read( s, 8 );
943 i_processed_length += 2;
948 /* ETSI 300 743 [7.2.1] */
949 static void decode_display_definition( decoder_t *p_dec, bs_t *s )
951 decoder_sys_t *p_sys = p_dec->p_sys;
952 uint16_t i_segment_length;
953 uint16_t i_processed_length = 40;
954 dvbsub_display_t *p_display;
955 dvbsub_display_t *p_old = p_sys->p_display;
958 i_segment_length = bs_read( s, 16 );
959 i_version = bs_read( s, 4 );
961 /* Check version number */
962 if( p_old && ( p_old->i_version == i_version ) )
964 /* The definition did not change */
965 bs_skip( s, 8*i_segment_length - 4 );
970 msg_Dbg( p_dec, "new display definition: %i", i_version );
972 p_display = malloc( sizeof(dvbsub_display_t) );
975 /* We don't have this version of the display definition: Parse it */
976 p_display->i_version = i_version;
977 p_display->b_windowed = bs_read( s, 1 );
978 bs_skip( s, 3 ); /* Reserved bits */
979 p_display->i_width = bs_read( s, 16 )+1;
980 p_display->i_height = bs_read( s, 16 )+1;
982 if( p_display->b_windowed )
985 msg_Dbg( p_dec, "display definition with offsets (windowed)" );
987 /* Coordinates are measured from the top left corner */
988 p_display->i_x = bs_read( s, 16 );
989 p_display->i_max_x = bs_read( s, 16 );
990 p_display->i_y = bs_read( s, 16 );
991 p_display->i_max_y = bs_read( s, 16 );
992 i_processed_length += 64;
996 p_sys->p_display = p_display;
999 if( i_processed_length != i_segment_length*8 )
1001 msg_Err( p_dec, "processed length %d != segment length %d",
1002 i_processed_length, i_segment_length );
1006 msg_Dbg( p_dec, "version: %d, width: %d, height: %d",
1007 p_display->i_version, p_display->i_width, p_display->i_height );
1008 if( p_display->b_windowed )
1009 msg_Dbg( p_dec, "xmin: %d, xmax: %d, ymin: %d, ymax: %d",
1010 p_display->i_x, p_display->i_max_x, p_display->i_y, p_display->i_max_y );
1014 static void dvbsub_render_pdata( decoder_t *, dvbsub_region_t *, int, int,
1016 static void dvbsub_pdata2bpp( bs_t *, uint8_t *, int, int * );
1017 static void dvbsub_pdata4bpp( bs_t *, uint8_t *, int, int * );
1018 static void dvbsub_pdata8bpp( bs_t *, uint8_t *, int, int * );
1020 static void decode_object( decoder_t *p_dec, bs_t *s )
1022 decoder_sys_t *p_sys = p_dec->p_sys;
1023 dvbsub_region_t *p_region;
1024 int i_segment_length, i_coding_method, i_version, i_id, i;
1025 bool b_non_modify_color;
1027 /* ETSI 300-743 paragraph 7.2.4
1028 * sync_byte, segment_type and page_id have already been processed.
1030 i_segment_length = bs_read( s, 16 );
1031 i_id = bs_read( s, 16 );
1032 i_version = bs_read( s, 4 );
1033 i_coding_method = bs_read( s, 2 );
1035 if( i_coding_method > 1 )
1037 msg_Dbg( p_dec, "unknown DVB subtitling coding %d is not handled!", i_coding_method );
1038 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
1042 /* Check if the object needs to be rendered in at least one
1044 for( p_region = p_sys->p_regions; p_region != NULL;
1045 p_region = p_region->p_next )
1047 for( i = 0; i < p_region->i_object_defs; i++ )
1048 if( p_region->p_object_defs[i].i_id == i_id ) break;
1050 if( i != p_region->i_object_defs ) break;
1054 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
1059 msg_Dbg( p_dec, "new object: %i", i_id );
1062 b_non_modify_color = bs_read( s, 1 );
1063 bs_skip( s, 1 ); /* Reserved */
1065 if( i_coding_method == 0x00 )
1067 int i_topfield, i_bottomfield;
1068 uint8_t *p_topfield, *p_bottomfield;
1070 i_topfield = bs_read( s, 16 );
1071 i_bottomfield = bs_read( s, 16 );
1072 p_topfield = s->p_start + bs_pos( s ) / 8;
1073 p_bottomfield = p_topfield + i_topfield;
1075 bs_skip( s, 8 * (i_segment_length - 7) );
1078 if( ( i_segment_length < ( i_topfield + i_bottomfield + 7 ) ) ||
1079 ( ( p_topfield + i_topfield + i_bottomfield ) > s->p_end ) )
1081 msg_Dbg( p_dec, "corrupted object data" );
1085 for( p_region = p_sys->p_regions; p_region != NULL;
1086 p_region = p_region->p_next )
1088 for( i = 0; i < p_region->i_object_defs; i++ )
1090 if( p_region->p_object_defs[i].i_id != i_id ) continue;
1092 dvbsub_render_pdata( p_dec, p_region,
1093 p_region->p_object_defs[i].i_x,
1094 p_region->p_object_defs[i].i_y,
1095 p_topfield, i_topfield );
1099 dvbsub_render_pdata( p_dec, p_region,
1100 p_region->p_object_defs[i].i_x,
1101 p_region->p_object_defs[i].i_y + 1,
1102 p_bottomfield, i_bottomfield );
1106 /* Duplicate the top field */
1107 dvbsub_render_pdata( p_dec, p_region,
1108 p_region->p_object_defs[i].i_x,
1109 p_region->p_object_defs[i].i_y + 1,
1110 p_topfield, i_topfield );
1117 /* DVB subtitling as characters */
1118 int i_number_of_codes = bs_read( s, 8 );
1119 uint8_t* p_start = s->p_start + bs_pos( s ) / 8;
1122 if( ( i_segment_length < ( i_number_of_codes*2 + 4 ) ) ||
1123 ( ( p_start + i_number_of_codes*2 ) > s->p_end ) )
1125 msg_Dbg( p_dec, "corrupted object data" );
1129 for( p_region = p_sys->p_regions; p_region != NULL;
1130 p_region = p_region->p_next )
1132 for( i = 0; i < p_region->i_object_defs; i++ )
1136 if( p_region->p_object_defs[i].i_id != i_id ) continue;
1138 p_region->p_object_defs[i].psz_text =
1139 realloc( p_region->p_object_defs[i].psz_text,
1140 i_number_of_codes + 1 );
1142 /* FIXME 16bits -> char ??? See Preamble */
1143 for( j = 0; j < i_number_of_codes; j++ )
1145 p_region->p_object_defs[i].psz_text[j] = (char)(bs_read( s, 16 ) & 0xFF);
1147 /* Null terminate the string */
1148 p_region->p_object_defs[i].psz_text[j] = 0;
1154 msg_Dbg( p_dec, "end object: %i", i_id );
1158 static void dvbsub_render_pdata( decoder_t *p_dec, dvbsub_region_t *p_region,
1160 uint8_t *p_field, int i_field )
1167 if( !p_region->p_pixbuf )
1169 msg_Err( p_dec, "region %i has no pixel buffer!", p_region->i_id );
1172 if( i_y < 0 || i_x < 0 || i_y >= p_region->i_height ||
1173 i_x >= p_region->i_width )
1175 msg_Dbg( p_dec, "invalid offset (%i,%i)", i_x, i_y );
1179 p_pixbuf = p_region->p_pixbuf + i_y * p_region->i_width;
1180 bs_init( &bs, p_field, i_field );
1182 while( !bs_eof( &bs ) )
1185 if( i_y >= p_region->i_height ) return;
1187 switch( bs_read( &bs, 8 ) )
1190 dvbsub_pdata2bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1195 dvbsub_pdata4bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1200 dvbsub_pdata8bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1207 /* We don't use map tables */
1210 case 0xf0: /* End of line code */
1211 p_pixbuf += 2*p_region->i_width;
1212 i_offset = 0; i_y += 2;
1218 static void dvbsub_pdata2bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1220 bool b_stop = false;
1222 while( !b_stop && !bs_eof( s ) )
1224 int i_count = 0, i_color = 0;
1226 i_color = bs_read( s, 2 );
1227 if( i_color != 0x00 )
1233 if( bs_read( s, 1 ) == 0x01 ) // Switch1
1235 i_count = 3 + bs_read( s, 3 );
1236 i_color = bs_read( s, 2 );
1240 if( bs_read( s, 1 ) == 0x00 ) //Switch2
1242 switch( bs_read( s, 2 ) ) //Switch3
1251 i_count = 12 + bs_read( s, 4 );
1252 i_color = bs_read( s, 2 );
1255 i_count = 29 + bs_read( s, 8 );
1256 i_color = bs_read( s, 2 );
1264 /* 1 pixel color 0 */
1270 if( !i_count ) continue;
1273 if( ( i_count + *pi_off ) > i_width ) break;
1275 if( i_count == 1 ) p[*pi_off] = i_color;
1276 else memset( ( p + *pi_off ), i_color, i_count );
1278 (*pi_off) += i_count;
1284 static void dvbsub_pdata4bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1286 bool b_stop = false;
1288 while( !b_stop && !bs_eof( s ) )
1290 int i_count = 0, i_color = 0;
1292 i_color = bs_read( s, 4 );
1293 if( i_color != 0x00 )
1300 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1302 if( bs_show( s, 3 ) != 0x00 )
1304 i_count = 2 + bs_read( s, 3 );
1314 if( bs_read( s, 1 ) == 0x00) //Switch2
1316 i_count = 4 + bs_read( s, 2 );
1317 i_color = bs_read( s, 4 );
1321 switch ( bs_read( s, 2 ) ) //Switch3
1330 i_count = 9 + bs_read( s, 4 );
1331 i_color = bs_read( s, 4 );
1334 i_count= 25 + bs_read( s, 8 );
1335 i_color = bs_read( s, 4 );
1342 if( !i_count ) continue;
1345 if( ( i_count + *pi_off ) > i_width ) break;
1347 if( i_count == 1 ) p[*pi_off] = i_color;
1348 else memset( ( p + *pi_off ), i_color, i_count );
1350 (*pi_off) += i_count;
1356 static void dvbsub_pdata8bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1358 bool b_stop = false;
1360 while( !b_stop && !bs_eof( s ) )
1362 int i_count = 0, i_color = 0;
1364 i_color = bs_read( s, 8 );
1365 if( i_color != 0x00 )
1372 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1374 if( bs_show( s, 7 ) != 0x00 )
1376 i_count = bs_read( s, 7 );
1386 i_count = bs_read( s, 7 );
1387 i_color = bs_read( s, 8 );
1391 if( !i_count ) continue;
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;
1405 static void free_all( decoder_t *p_dec )
1407 decoder_sys_t *p_sys = p_dec->p_sys;
1408 dvbsub_region_t *p_reg, *p_reg_next;
1409 dvbsub_clut_t *p_clut, *p_clut_next;
1411 free( p_sys->p_display );
1413 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut_next )
1415 p_clut_next = p_clut->p_next;
1418 p_sys->p_cluts = NULL;
1420 for( p_reg = p_sys->p_regions; p_reg != NULL; p_reg = p_reg_next )
1424 p_reg_next = p_reg->p_next;
1425 for( i = 0; i < p_reg->i_object_defs; i++ )
1426 free( p_reg->p_object_defs[i].psz_text );
1427 if( p_reg->i_object_defs ) free( p_reg->p_object_defs );
1428 free( p_reg->p_pixbuf );
1431 p_sys->p_regions = NULL;
1435 if( p_sys->p_page->i_region_defs )
1436 free( p_sys->p_page->p_region_defs );
1437 free( p_sys->p_page );
1439 p_sys->p_page = NULL;
1442 static subpicture_t *render( decoder_t *p_dec )
1444 decoder_sys_t *p_sys = p_dec->p_sys;
1445 subpicture_t *p_spu;
1446 subpicture_region_t **pp_spu_region;
1447 int i, j, i_timeout = 0;
1451 /* Allocate the subpicture internal data. */
1452 p_spu = decoder_NewSubpicture( p_dec );
1456 p_spu->b_absolute = p_sys->b_absolute;
1457 /* Set the pf_render callback */
1458 p_spu->i_start = (mtime_t) p_sys->i_pts;
1459 //p_spu->i_stop = (mtime_t) 0;
1460 p_spu->b_ephemer = true;
1461 //p_spu->b_fade = true;
1462 //p_spu->i_stop = p_spu->i_start + (mtime_t) (i_timeout * 1000000);
1464 /* Correct positioning of SPU */
1465 i_base_x = p_sys->i_spu_x;
1466 i_base_y = p_sys->i_spu_y;
1467 p_spu->i_original_picture_width = 720;
1468 p_spu->i_original_picture_height = 576;
1470 if( p_sys->p_display )
1472 p_spu->i_original_picture_width = p_sys->p_display->i_width;
1473 p_spu->i_original_picture_height = p_sys->p_display->i_height;
1475 if( p_sys->p_display->b_windowed )
1477 /* TODO: check that this actually works */
1478 p_spu->i_original_picture_width = p_sys->p_display->i_max_x - p_sys->p_display->i_x;
1479 p_spu->i_original_picture_height = p_sys->p_display->i_max_y - p_sys->p_display->i_y;
1480 i_base_x += p_sys->p_display->i_x;
1481 i_base_y += p_sys->p_display->i_y;
1485 pp_spu_region = &p_spu->p_region;
1487 /* Loop on region definitions */
1490 msg_Dbg( p_dec, "rendering %i regions", p_sys->p_page->i_region_defs );
1493 for( i = 0; p_sys->p_page && ( i < p_sys->p_page->i_region_defs ); i++ )
1495 dvbsub_region_t *p_region;
1496 dvbsub_regiondef_t *p_regiondef;
1497 dvbsub_clut_t *p_clut;
1498 dvbsub_color_t *p_color;
1499 subpicture_region_t *p_spu_region;
1500 uint8_t *p_src, *p_dst;
1502 video_palette_t palette;
1505 i_timeout = p_sys->p_page->i_timeout;
1507 p_regiondef = &p_sys->p_page->p_region_defs[i];
1510 msg_Dbg( p_dec, "rendering region %i (%i,%i)", i,
1511 p_regiondef->i_x, p_regiondef->i_y );
1514 /* Find associated region */
1515 for( p_region = p_sys->p_regions; p_region != NULL;
1516 p_region = p_region->p_next )
1518 if( p_regiondef->i_id == p_region->i_id ) break;
1523 msg_Dbg( p_dec, "region %i not found", p_regiondef->i_id );
1527 /* Find associated CLUT */
1528 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next )
1530 if( p_region->i_clut == p_clut->i_id ) break;
1534 msg_Dbg( p_dec, "clut %i not found", p_region->i_clut );
1538 /* FIXME: don't create a subpicture region with VLC_FOURCC YUVP
1539 * when it actually is a TEXT region */
1541 /* Create new SPU region */
1542 memset( &fmt, 0, sizeof(video_format_t) );
1543 fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
1544 fmt.i_aspect = 0; /* 0 means use aspect ratio of background video */
1545 fmt.i_width = fmt.i_visible_width = p_region->i_width;
1546 fmt.i_height = fmt.i_visible_height = p_region->i_height;
1547 fmt.i_x_offset = fmt.i_y_offset = 0;
1548 fmt.p_palette = &palette;
1549 fmt.p_palette->i_entries = ( p_region->i_depth == 1 ) ? 4 :
1550 ( ( p_region->i_depth == 2 ) ? 16 : 256 );
1551 p_color = ( p_region->i_depth == 1 ) ? p_clut->c_2b :
1552 ( ( p_region->i_depth == 2 ) ? p_clut->c_4b : p_clut->c_8b );
1553 for( j = 0; j < fmt.p_palette->i_entries; j++ )
1555 fmt.p_palette->palette[j][0] = p_color[j].Y;
1556 fmt.p_palette->palette[j][1] = p_color[j].Cb; /* U == Cb */
1557 fmt.p_palette->palette[j][2] = p_color[j].Cr; /* V == Cr */
1558 fmt.p_palette->palette[j][3] = 0xff - p_color[j].T;
1561 p_spu_region = subpicture_region_New( &fmt );
1564 msg_Err( p_dec, "cannot allocate SPU region" );
1567 p_spu_region->i_x = i_base_x + p_regiondef->i_x;
1568 p_spu_region->i_y = i_base_y + p_regiondef->i_y;
1569 p_spu_region->i_align = p_sys->i_spu_position;
1570 *pp_spu_region = p_spu_region;
1571 pp_spu_region = &p_spu_region->p_next;
1573 p_src = p_region->p_pixbuf;
1574 p_dst = p_spu_region->p_picture->Y_PIXELS;
1575 i_pitch = p_spu_region->p_picture->Y_PITCH;
1577 /* Copy pixel buffer */
1578 for( j = 0; j < p_region->i_height; j++ )
1580 memcpy( p_dst, p_src, p_region->i_width );
1581 p_src += p_region->i_width;
1585 /* Check subtitles encoded as strings of characters
1586 * (since there are not rendered in the pixbuffer) */
1587 for( j = 0; j < p_region->i_object_defs; j++ )
1589 dvbsub_objectdef_t *p_object_def = &p_region->p_object_defs[j];
1591 if( ( p_object_def->i_type != 1 ) || !p_object_def->psz_text )
1594 /* Create new SPU region */
1595 memset( &fmt, 0, sizeof(video_format_t) );
1596 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
1597 fmt.i_aspect = VOUT_ASPECT_FACTOR;
1598 fmt.i_width = fmt.i_visible_width = p_region->i_width;
1599 fmt.i_height = fmt.i_visible_height = p_region->i_height;
1600 fmt.i_x_offset = fmt.i_y_offset = 0;
1601 p_spu_region = subpicture_region_New( &fmt );
1604 msg_Err( p_dec, "cannot allocate SPU region" );
1608 p_spu_region->psz_text = strdup( p_object_def->psz_text );
1609 p_spu_region->i_x = i_base_x + p_regiondef->i_x + p_object_def->i_x;
1610 p_spu_region->i_y = i_base_y + p_regiondef->i_y + p_object_def->i_y;
1611 p_spu_region->i_align = p_sys->i_spu_position;
1612 *pp_spu_region = p_spu_region;
1613 pp_spu_region = &p_spu_region->p_next;
1620 /*****************************************************************************
1621 * encoder_sys_t : encoder descriptor
1622 *****************************************************************************/
1623 typedef struct encoder_region_t
1630 struct encoder_sys_t
1632 unsigned int i_page_ver;
1633 unsigned int i_region_ver;
1634 unsigned int i_clut_ver;
1637 encoder_region_t *p_regions;
1641 /* subpicture positioning */
1646 static void encode_page_composition( encoder_t *, bs_t *, subpicture_t * );
1647 static void encode_clut( encoder_t *, bs_t *, subpicture_t * );
1648 static void encode_region_composition( encoder_t *, bs_t *, subpicture_t * );
1649 static void encode_object( encoder_t *, bs_t *, subpicture_t * );
1651 /*****************************************************************************
1652 * OpenEncoder: probe the encoder and return score
1653 *****************************************************************************/
1654 static int OpenEncoder( vlc_object_t *p_this )
1656 encoder_t *p_enc = (encoder_t *)p_this;
1657 encoder_sys_t *p_sys;
1660 if( ( p_enc->fmt_out.i_codec != VLC_FOURCC('d','v','b','s') ) &&
1663 return VLC_EGENERIC;
1666 /* Allocate the memory needed to store the decoder's structure */
1667 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1669 p_enc->p_sys = p_sys;
1671 p_enc->pf_encode_sub = Encode;
1672 p_enc->fmt_out.i_codec = VLC_FOURCC('d','v','b','s');
1673 p_enc->fmt_out.subs.dvb.i_id = 1 << 16 | 1;
1675 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
1677 p_sys->i_page_ver = 0;
1678 p_sys->i_region_ver = 0;
1679 p_sys->i_clut_ver = 0;
1680 p_sys->i_regions = 0;
1681 p_sys->p_regions = 0;
1683 var_Create( p_this, ENC_CFG_PREFIX "x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
1684 var_Get( p_this, ENC_CFG_PREFIX "x", &val );
1685 p_sys->i_offset_x = val.i_int;
1686 var_Create( p_this, ENC_CFG_PREFIX "y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
1687 var_Get( p_this, ENC_CFG_PREFIX "y", &val );
1688 p_sys->i_offset_y = val.i_int;
1693 /* FIXME: this routine is a hack to convert VLC_FOURCC('Y','U','V','A')
1694 * into VLC_FOURCC('Y','U','V','P')
1696 static subpicture_t *YuvaYuvp( subpicture_t *p_subpic )
1698 subpicture_region_t *p_region = NULL;
1700 if( !p_subpic ) return NULL;
1702 for( p_region = p_subpic->p_region; p_region; p_region = p_region->p_next )
1704 video_format_t *p_fmt = &p_region->fmt;
1705 int i = 0, j = 0, n = 0, p = 0;
1706 int i_max_entries = 256;
1708 #ifdef RANDOM_DITHERING
1709 int i_seed = 0xdeadbeef; /* random seed */
1713 int i_pixels = p_region->p_picture->p[0].i_visible_lines
1714 * p_region->p_picture->p[0].i_pitch;
1715 int i_iterator = p_region->p_picture->p[0].i_visible_lines * 3 / 4
1716 * p_region->p_picture->p[0].i_pitch
1717 + p_region->p_picture->p[0].i_pitch * 1 / 3;
1718 int i_tolerance = 0;
1721 msg_Dbg( p_enc, "YuvaYuvp: i_pixels=%d, i_iterator=%d", i_pixels, i_iterator );
1723 p_fmt->i_chroma = VLC_FOURCC('Y','U','V','P');
1724 p_fmt->p_palette = (video_palette_t *) malloc( sizeof( video_palette_t ) );
1725 if( !p_fmt->p_palette ) break;
1726 p_fmt->p_palette->i_entries = 0;
1728 /* Find best iterator using Euclide’s algorithm */
1729 for( ; i_iterator > 1 ; i_iterator-- )
1748 /* Count colors, build best palette */
1749 for( i_tolerance = 0; i_tolerance < 128; i_tolerance++ )
1751 bool b_success = true;
1752 p_fmt->p_palette->i_entries = 0;
1754 for( i = 0; i < i_pixels ; )
1757 y = p_region->p_picture->p[0].p_pixels[i];
1758 u = p_region->p_picture->p[1].p_pixels[i];
1759 v = p_region->p_picture->p[2].p_pixels[i];
1760 a = p_region->p_picture->p[3].p_pixels[i];
1761 for( j = 0; j < p_fmt->p_palette->i_entries; j++ )
1763 if( abs((int)p_fmt->p_palette->palette[j][0] - (int)y) <= i_tolerance &&
1764 abs((int)p_fmt->p_palette->palette[j][1] - (int)u) <= i_tolerance &&
1765 abs((int)p_fmt->p_palette->palette[j][2] - (int)v) <= i_tolerance &&
1766 abs((int)p_fmt->p_palette->palette[j][3] - (int)a) <= i_tolerance / 2 )
1771 if( j == p_fmt->p_palette->i_entries )
1773 p_fmt->p_palette->palette[j][0] = y;
1774 p_fmt->p_palette->palette[j][1] = u;
1775 p_fmt->p_palette->palette[j][2] = v;
1776 p_fmt->p_palette->palette[j][3] = a;
1777 p_fmt->p_palette->i_entries++;
1779 if( p_fmt->p_palette->i_entries >= i_max_entries )
1798 msg_Dbg( p_enc, "best palette has %d colors", p_fmt->p_palette->i_entries );
1801 #ifndef RANDOM_DITHERING
1802 pi_delta = malloc( ( p_region->p_picture->p[0].i_pitch + 1 )
1803 * sizeof(int) * 4 );
1804 for( i = 0; i < (p_region->p_picture->p[0].i_pitch + 1) * 4 ; i++ )
1810 /* Fill image with our new colours */
1811 for( p = 0; p < p_region->p_picture->p[0].i_visible_lines ; p++ )
1813 int i_ydelta = 0, i_udelta = 0, i_vdelta = 0, i_adelta = 0;
1815 for( n = 0; n < p_region->p_picture->p[0].i_pitch ; n++ )
1817 int i_offset = p * p_region->p_picture->p[0].i_pitch + n;
1819 int i_mindist, i_best;
1821 y = (int)p_region->p_picture->p[0].p_pixels[i_offset];
1822 u = (int)p_region->p_picture->p[1].p_pixels[i_offset];
1823 v = (int)p_region->p_picture->p[2].p_pixels[i_offset];
1824 a = (int)p_region->p_picture->p[3].p_pixels[i_offset];
1826 /* Add dithering compensation */
1827 #ifdef RANDOM_DITHERING
1828 y += ((i_seed & 0xff) - 0x80) * i_tolerance / 0x80;
1829 u += (((i_seed >> 8) & 0xff) - 0x80) * i_tolerance / 0x80;
1830 v += (((i_seed >> 16) & 0xff) - 0x80) * i_tolerance / 0x80;
1831 a += (((i_seed >> 24) & 0xff) - 0x80) * i_tolerance / 0x80;
1833 y += i_ydelta + pi_delta[ n * 4 ];
1834 u += i_udelta + pi_delta[ n * 4 + 1 ];
1835 v += i_vdelta + pi_delta[ n * 4 + 2 ];
1836 a += i_adelta + pi_delta[ n * 4 + 3 ];
1839 /* Find best colour in palette */
1840 for( i_mindist = 99999999, i_best = 0, j = 0; j < p_fmt->p_palette->i_entries; j++ )
1844 i_dist += abs((int)p_fmt->p_palette->palette[j][0] - y);
1845 i_dist += abs((int)p_fmt->p_palette->palette[j][1] - u);
1846 i_dist += abs((int)p_fmt->p_palette->palette[j][2] - v);
1847 i_dist += 2 * abs((int)p_fmt->p_palette->palette[j][3] - a);
1849 if( i_dist < i_mindist )
1856 /* Set pixel to best color */
1857 p_region->p_picture->p[0].p_pixels[i_offset] = i_best;
1859 /* Update dithering state */
1860 #ifdef RANDOM_DITHERING
1861 i_seed = (i_seed * 0x1283837) ^ 0x789479 ^ (i_seed >> 13);
1863 i_ydelta = y - (int)p_fmt->p_palette->palette[i_best][0];
1864 i_udelta = u - (int)p_fmt->p_palette->palette[i_best][1];
1865 i_vdelta = v - (int)p_fmt->p_palette->palette[i_best][2];
1866 i_adelta = a - (int)p_fmt->p_palette->palette[i_best][3];
1867 pi_delta[ n * 4 ] = i_ydelta * 3 / 8;
1868 pi_delta[ n * 4 + 1 ] = i_udelta * 3 / 8;
1869 pi_delta[ n * 4 + 2 ] = i_vdelta * 3 / 8;
1870 pi_delta[ n * 4 + 3 ] = i_adelta * 3 / 8;
1871 i_ydelta = i_ydelta * 5 / 8;
1872 i_udelta = i_udelta * 5 / 8;
1873 i_vdelta = i_vdelta * 5 / 8;
1874 i_adelta = i_adelta * 5 / 8;
1878 #ifndef RANDOM_DITHERING
1883 for( i = p_fmt->p_palette->i_entries; i < i_max_entries; i++ )
1885 p_fmt->p_palette->palette[i][0] = 0;
1886 p_fmt->p_palette->palette[i][1] = 0;
1887 p_fmt->p_palette->palette[i][2] = 0;
1888 p_fmt->p_palette->palette[i][3] = 0;
1890 p_fmt->p_palette->i_entries = i_max_entries;
1892 msg_Dbg( p_enc, "best palette has %d colors", p_fmt->p_palette->i_entries );
1898 /****************************************************************************
1899 * Encode: the whole thing
1900 ****************************************************************************/
1901 static block_t *Encode( encoder_t *p_enc, subpicture_t *p_subpic )
1903 subpicture_t *p_temp = NULL;
1904 subpicture_region_t *p_region = NULL;
1905 bs_t bits, *s = &bits;
1908 if( !p_subpic || !p_subpic->p_region ) return NULL;
1910 /* FIXME: this is a hack to convert VLC_FOURCC('Y','U','V','A') into
1911 * VLC_FOURCC('Y','U','V','P')
1913 p_region = p_subpic->p_region;
1914 if( p_region->fmt.i_chroma == VLC_FOURCC('Y','U','V','A') )
1916 p_temp = YuvaYuvp( p_subpic );
1919 msg_Err( p_enc, "no picture in subpicture" );
1922 p_region = p_subpic->p_region;
1926 if( !p_region ) return NULL;
1928 if( ( p_region->fmt.i_chroma != VLC_FOURCC('T','E','X','T') ) &&
1929 ( p_region->fmt.i_chroma != VLC_FOURCC('Y','U','V','P') ) )
1932 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
1933 vlc_fourcc_to_char( p_region->fmt.i_chroma, &psz_fourcc );
1934 msg_Err( p_enc, "chroma %4s not supported", psz_fourcc );
1938 if( p_region->fmt.p_palette )
1940 switch( p_region->fmt.p_palette->i_entries )
1948 msg_Err( p_enc, "subpicture palette (%d) not handled",
1949 p_region->fmt.p_palette->i_entries );
1956 msg_Dbg( p_enc, "encoding subpicture" );
1958 p_block = block_New( p_enc, 64000 );
1959 bs_init( s, p_block->p_buffer, p_block->i_buffer );
1961 bs_write( s, 8, 0x20 ); /* Data identifier */
1962 bs_write( s, 8, 0x0 ); /* Subtitle stream id */
1964 encode_page_composition( p_enc, s, p_subpic );
1965 encode_region_composition( p_enc, s, p_subpic );
1966 encode_clut( p_enc, s, p_subpic );
1967 encode_object( p_enc, s, p_subpic );
1969 /* End of display */
1970 bs_write( s, 8, 0x0f ); /* Sync byte */
1971 bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
1972 bs_write( s, 16, 1 ); /* Page id */
1973 bs_write( s, 16, 0 ); /* Segment length */
1975 bs_write( s, 8, 0xff );/* End marker */
1976 p_block->i_buffer = bs_pos( s ) / 8;
1977 p_block->i_pts = p_block->i_dts = p_subpic->i_start;
1978 if( !p_subpic->b_ephemer && ( p_subpic->i_stop > p_subpic->i_start ) )
1980 block_t *p_block_stop;
1982 p_block->i_length = p_subpic->i_stop - p_subpic->i_start;
1984 /* Send another (empty) subtitle to signal the end of display */
1985 p_block_stop = block_New( p_enc, 64000 );
1986 bs_init( s, p_block_stop->p_buffer, p_block_stop->i_buffer );
1987 bs_write( s, 8, 0x20 ); /* Data identifier */
1988 bs_write( s, 8, 0x0 ); /* Subtitle stream id */
1989 encode_page_composition( p_enc, s, 0 );
1990 bs_write( s, 8, 0x0f ); /* Sync byte */
1991 bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
1992 bs_write( s, 16, 1 ); /* Page id */
1993 bs_write( s, 16, 0 ); /* Segment length */
1994 bs_write( s, 8, 0xff );/* End marker */
1995 p_block_stop->i_buffer = bs_pos( s ) / 8;
1996 p_block_stop->i_pts = p_block_stop->i_dts = p_subpic->i_stop;
1997 block_ChainAppend( &p_block, p_block_stop );
1998 p_block_stop->i_length = 100000; /* p_subpic->i_stop - p_subpic->i_start; */
2001 msg_Dbg( p_enc, "subpicture encoded properly" );
2006 /*****************************************************************************
2007 * CloseEncoder: encoder destruction
2008 *****************************************************************************/
2009 static void CloseEncoder( vlc_object_t *p_this )
2011 encoder_t *p_enc = (encoder_t *)p_this;
2012 encoder_sys_t *p_sys = p_enc->p_sys;
2014 var_Destroy( p_this , ENC_CFG_PREFIX "x" );
2015 var_Destroy( p_this , ENC_CFG_PREFIX "y" );
2016 var_Destroy( p_this , ENC_CFG_PREFIX "timeout" );
2018 if( p_sys->i_regions ) free( p_sys->p_regions );
2022 static void encode_page_composition( encoder_t *p_enc, bs_t *s,
2023 subpicture_t *p_subpic )
2025 encoder_sys_t *p_sys = p_enc->p_sys;
2026 subpicture_region_t *p_region;
2027 bool b_mode_change = false;
2028 int i_regions, i_timeout;
2030 bs_write( s, 8, 0x0f ); /* Sync byte */
2031 bs_write( s, 8, DVBSUB_ST_PAGE_COMPOSITION ); /* Segment type */
2032 bs_write( s, 16, 1 ); /* Page id */
2034 for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
2035 p_region; p_region = p_region->p_next, i_regions++ )
2037 if( i_regions >= p_sys->i_regions )
2039 encoder_region_t region;
2040 region.i_width = region.i_height = 0;
2042 realloc( p_sys->p_regions, sizeof(encoder_region_t) *
2043 (p_sys->i_regions + 1) );
2044 p_sys->p_regions[p_sys->i_regions++] = region;
2047 if( ( p_sys->p_regions[i_regions].i_width <
2048 (int)p_region->fmt.i_visible_width ) ||
2049 ( p_sys->p_regions[i_regions].i_width >
2050 (int)p_region->fmt.i_visible_width ) )
2052 b_mode_change = true;
2053 msg_Dbg( p_enc, "region %i width change: %i -> %i",
2054 i_regions, p_sys->p_regions[i_regions].i_width,
2055 p_region->fmt.i_visible_width );
2056 p_sys->p_regions[i_regions].i_width =
2057 p_region->fmt.i_visible_width;
2059 if( p_sys->p_regions[i_regions].i_height <
2060 (int)p_region->fmt.i_visible_height )
2062 b_mode_change = true;
2063 msg_Dbg( p_enc, "region %i height change: %i -> %i",
2064 i_regions, p_sys->p_regions[i_regions].i_height,
2065 p_region->fmt.i_visible_height );
2066 p_sys->p_regions[i_regions].i_height =
2067 p_region->fmt.i_visible_height;
2071 bs_write( s, 16, i_regions * 6 + 2 ); /* Segment length */
2074 if( p_subpic && !p_subpic->b_ephemer &&
2075 ( p_subpic->i_stop > p_subpic->i_start ) )
2077 i_timeout = (p_subpic->i_stop - p_subpic->i_start) / 1000000;
2080 bs_write( s, 8, i_timeout ); /* Timeout */
2081 bs_write( s, 4, p_sys->i_page_ver++ );
2082 bs_write( s, 2, b_mode_change ?
2083 DVBSUB_PCS_STATE_CHANGE : DVBSUB_PCS_STATE_ACQUISITION );
2084 bs_write( s, 2, 0 ); /* Reserved */
2086 for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
2087 p_region; p_region = p_region->p_next, i_regions++ )
2089 bs_write( s, 8, i_regions );
2090 bs_write( s, 8, 0 ); /* Reserved */
2091 if( (p_sys->i_offset_x > 0) && (p_sys->i_offset_y > 0) )
2093 bs_write( s, 16, p_sys->i_offset_x ); /* override x position */
2094 bs_write( s, 16, p_sys->i_offset_y ); /* override y position */
2098 bs_write( s, 16, p_region->i_x );
2099 bs_write( s, 16, p_region->i_y );
2104 static void encode_clut( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
2106 encoder_sys_t *p_sys = p_enc->p_sys;
2107 subpicture_region_t *p_region = p_subpic->p_region;
2108 video_palette_t *p_pal, pal;
2112 if( !p_region ) return;
2114 if( p_region->fmt.i_chroma == VLC_FOURCC('Y','U','V','P') )
2116 p_pal = p_region->fmt.p_palette;
2121 for( i = 0; i < 4; i++ )
2123 pal.palette[i][0] = 0;
2124 pal.palette[i][1] = 0;
2125 pal.palette[i][2] = 0;
2126 pal.palette[i][3] = 0;
2131 bs_write( s, 8, 0x0f ); /* Sync byte */
2132 bs_write( s, 8, DVBSUB_ST_CLUT_DEFINITION ); /* Segment type */
2133 bs_write( s, 16, 1 ); /* Page id */
2135 bs_write( s, 16, p_pal->i_entries * 6 + 2 ); /* Segment length */
2136 bs_write( s, 8, 1 ); /* Clut id */
2137 bs_write( s, 4, p_sys->i_clut_ver++ );
2138 bs_write( s, 4, 0 ); /* Reserved */
2140 for( i = 0; i < p_pal->i_entries; i++ )
2142 bs_write( s, 8, i ); /* Clut entry id */
2143 bs_write( s, 1, p_pal->i_entries == 4 ); /* 2bit/entry flag */
2144 bs_write( s, 1, p_pal->i_entries == 16 ); /* 4bit/entry flag */
2145 bs_write( s, 1, p_pal->i_entries == 256 ); /* 8bit/entry flag */
2146 bs_write( s, 4, 0 ); /* Reserved */
2147 bs_write( s, 1, 1 ); /* Full range flag */
2148 bs_write( s, 8, p_pal->palette[i][3] ? /* Y value */
2149 (p_pal->palette[i][0] ? p_pal->palette[i][0] : 16) : 0 );
2150 bs_write( s, 8, p_pal->palette[i][1] ); /* Cr value */
2151 bs_write( s, 8, p_pal->palette[i][2] ); /* Cb value */
2152 bs_write( s, 8, 0xff - p_pal->palette[i][3] ); /* T value */
2156 static void encode_region_composition( encoder_t *p_enc, bs_t *s,
2157 subpicture_t *p_subpic )
2159 encoder_sys_t *p_sys = p_enc->p_sys;
2160 subpicture_region_t *p_region;
2163 for( i_region = 0, p_region = p_subpic->p_region; p_region;
2164 p_region = p_region->p_next, i_region++ )
2166 int i_entries = 4, i_depth = 0x1, i_bg = 0;
2168 ( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') );
2172 video_palette_t *p_pal = p_region->fmt.p_palette;
2176 msg_Err( p_enc, "subpicture has no palette - ignoring it" );
2180 i_entries = p_pal->i_entries;
2181 i_depth = i_entries == 4 ? 0x1 : i_entries == 16 ? 0x2 : 0x3;
2183 for( i_bg = 0; i_bg < p_pal->i_entries; i_bg++ )
2185 if( !p_pal->palette[i_bg][3] ) break;
2189 bs_write( s, 8, 0x0f ); /* Sync byte */
2190 bs_write( s, 8, DVBSUB_ST_REGION_COMPOSITION ); /* Segment type */
2191 bs_write( s, 16, 1 ); /* Page id */
2193 bs_write( s, 16, 10 + 6 + (b_text ? 2 : 0) ); /* Segment length */
2194 bs_write( s, 8, i_region );
2195 bs_write( s, 4, p_sys->i_region_ver++ );
2197 /* Region attributes */
2198 bs_write( s, 1, i_bg < i_entries ); /* Fill */
2199 bs_write( s, 3, 0 ); /* Reserved */
2200 bs_write( s, 16, p_sys->p_regions[i_region].i_width );
2201 bs_write( s, 16, p_sys->p_regions[i_region].i_height );
2202 bs_write( s, 3, i_depth ); /* Region level of compatibility */
2203 bs_write( s, 3, i_depth ); /* Region depth */
2204 bs_write( s, 2, 0 ); /* Reserved */
2205 bs_write( s, 8, 1 ); /* Clut id */
2206 bs_write( s, 8, i_bg ); /* region 8bit pixel code */
2207 bs_write( s, 4, i_bg ); /* region 4bit pixel code */
2208 bs_write( s, 2, i_bg ); /* region 2bit pixel code */
2209 bs_write( s, 2, 0 ); /* Reserved */
2211 /* In our implementation we only have 1 object per region */
2212 bs_write( s, 16, i_region );
2213 bs_write( s, 2, b_text ? DVBSUB_OT_BASIC_CHAR:DVBSUB_OT_BASIC_BITMAP );
2214 bs_write( s, 2, 0 ); /* object provider flag */
2215 bs_write( s, 12, 0 );/* object horizontal position */
2216 bs_write( s, 4, 0 ); /* Reserved */
2217 bs_write( s, 12, 0 );/* object vertical position */
2221 bs_write( s, 8, 1 ); /* foreground pixel code */
2222 bs_write( s, 8, 0 ); /* background pixel code */
2227 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
2228 subpicture_region_t *p_region,
2231 static void encode_object( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
2233 encoder_sys_t *p_sys = p_enc->p_sys;
2234 subpicture_region_t *p_region;
2237 int i_length_pos, i_update_pos, i_pixel_data_pos;
2239 for( i_region = 0, p_region = p_subpic->p_region; p_region;
2240 p_region = p_region->p_next, i_region++ )
2242 bs_write( s, 8, 0x0f ); /* Sync byte */
2243 bs_write( s, 8, DVBSUB_ST_OBJECT_DATA ); /* Segment type */
2244 bs_write( s, 16, 1 ); /* Page id */
2246 i_length_pos = bs_pos( s );
2247 bs_write( s, 16, 0 ); /* Segment length */
2248 bs_write( s, 16, i_region ); /* Object id */
2249 bs_write( s, 4, p_sys->i_region_ver++ );
2251 /* object coding method */
2252 switch( p_region->fmt.i_chroma )
2254 case VLC_FOURCC( 'Y','U','V','P' ):
2255 bs_write( s, 2, 0 );
2257 case VLC_FOURCC( 'T','E','X','T' ):
2258 bs_write( s, 2, 1 );
2261 msg_Err( p_enc, "FOURCC %d not supported by encoder.", p_region->fmt.i_chroma );
2265 bs_write( s, 1, 0 ); /* non modifying color flag */
2266 bs_write( s, 1, 0 ); /* Reserved */
2268 if( p_region->fmt.i_chroma == VLC_FOURCC( 'T','E','X','T' ) )
2272 if( !p_region->psz_text ) continue;
2274 i_size = __MIN( strlen( p_region->psz_text ), 256 );
2276 bs_write( s, 8, i_size ); /* number of characters in string */
2277 for( i = 0; i < i_size; i++ )
2279 bs_write( s, 16, p_region->psz_text[i] );
2282 /* Update segment length */
2283 SetWBE( &s->p_start[i_length_pos/8],
2284 (bs_pos(s) - i_length_pos)/8 -2 );
2288 /* Coding of a bitmap object */
2289 i_update_pos = bs_pos( s );
2290 bs_write( s, 16, 0 ); /* topfield data block length */
2291 bs_write( s, 16, 0 ); /* bottomfield data block length */
2294 i_pixel_data_pos = bs_pos( s );
2295 encode_pixel_data( p_enc, s, p_region, true );
2296 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
2297 SetWBE( &s->p_start[i_update_pos/8], i_pixel_data_pos );
2300 i_pixel_data_pos = bs_pos( s );
2301 encode_pixel_data( p_enc, s, p_region, false );
2302 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
2303 SetWBE( &s->p_start[i_update_pos/8+2], i_pixel_data_pos );
2305 /* Stuffing for word alignment */
2307 if( bs_pos( s ) % 16 ) bs_write( s, 8, 0 );
2309 /* Update segment length */
2310 SetWBE( &s->p_start[i_length_pos/8], (bs_pos(s) - i_length_pos)/8 -2 );
2314 static void encode_pixel_line_2bp( bs_t *s, subpicture_region_t *p_region,
2316 static void encode_pixel_line_4bp( bs_t *s, subpicture_region_t *p_region,
2318 static void encode_pixel_line_8bp( bs_t *s, subpicture_region_t *p_region,
2320 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
2321 subpicture_region_t *p_region,
2324 unsigned int i_line;
2327 if( p_region->fmt.i_chroma != VLC_FOURCC('Y','U','V','P') ) return;
2329 /* Encode line by line */
2330 for( i_line = !b_top; i_line < p_region->fmt.i_visible_height;
2333 switch( p_region->fmt.p_palette->i_entries )
2339 bs_write( s, 8, 0x10 ); /* 2 bit/pixel code string */
2340 encode_pixel_line_2bp( s, p_region, i_line );
2344 bs_write( s, 8, 0x11 ); /* 4 bit/pixel code string */
2345 encode_pixel_line_4bp( s, p_region, i_line );
2349 bs_write( s, 8, 0x12 ); /* 8 bit/pixel code string */
2350 encode_pixel_line_8bp( s, p_region, i_line );
2354 msg_Err( p_enc, "subpicture palette (%i) not handled",
2355 p_region->fmt.p_palette->i_entries );
2359 bs_write( s, 8, 0xf0 ); /* End of object line code */
2363 static void encode_pixel_line_2bp( bs_t *s, subpicture_region_t *p_region,
2366 unsigned int i, i_length = 0;
2367 int i_pitch = p_region->p_picture->p->i_pitch;
2368 uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
2369 int i_last_pixel = p_data[0];
2371 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2373 if( ( i != p_region->fmt.i_visible_width ) &&
2374 ( p_data[i] == i_last_pixel ) && ( i_length != 284 ) )
2380 if( ( i_length == 1 ) || ( i_length == 11 ) || ( i_length == 28 ) )
2382 /* 2bit/pixel code */
2384 bs_write( s, 2, i_last_pixel );
2387 bs_write( s, 2, 0 );
2388 bs_write( s, 1, 0 );
2389 bs_write( s, 1, 1 ); /* pseudo color 0 */
2398 bs_write( s, 2, i_last_pixel );
2399 bs_write( s, 2, i_last_pixel );
2403 bs_write( s, 2, 0 );
2404 bs_write( s, 1, 0 );
2405 bs_write( s, 1, 0 );
2406 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
2409 else if( i_length > 2 )
2411 bs_write( s, 2, 0 );
2412 if( i_length <= 10 )
2414 bs_write( s, 1, 1 );
2415 bs_write( s, 3, i_length - 3 );
2416 bs_write( s, 2, i_last_pixel );
2420 bs_write( s, 1, 0 );
2421 bs_write( s, 1, 0 );
2423 if( i_length <= 27 )
2425 bs_write( s, 2, 2 );
2426 bs_write( s, 4, i_length - 12 );
2427 bs_write( s, 2, i_last_pixel );
2431 bs_write( s, 2, 3 );
2432 bs_write( s, 8, i_length - 29 );
2433 bs_write( s, 2, i_last_pixel );
2438 if( i == p_region->fmt.i_visible_width ) break;
2440 i_last_pixel = p_data[i];
2445 bs_write( s, 2, 0 );
2446 bs_write( s, 1, 0 );
2447 bs_write( s, 1, 0 );
2448 bs_write( s, 2, 0 );
2454 static void encode_pixel_line_4bp( bs_t *s, subpicture_region_t *p_region,
2457 unsigned int i, i_length = 0;
2458 int i_pitch = p_region->p_picture->p->i_pitch;
2459 uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
2460 int i_last_pixel = p_data[0];
2462 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2464 if( i != p_region->fmt.i_visible_width &&
2465 p_data[i] == i_last_pixel && i_length != 280 )
2471 if( ( i_length == 1 ) ||
2472 ( ( i_length == 3 ) && i_last_pixel ) ||
2475 /* 4bit/pixel code */
2477 bs_write( s, 4, i_last_pixel );
2480 bs_write( s, 4, 0 );
2481 bs_write( s, 1, 1 );
2482 bs_write( s, 1, 1 );
2483 bs_write( s, 2, 0 ); /* pseudo color 0 */
2492 bs_write( s, 4, i_last_pixel );
2493 bs_write( s, 4, i_last_pixel );
2497 bs_write( s, 4, 0 );
2498 bs_write( s, 1, 1 );
2499 bs_write( s, 1, 1 );
2500 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
2503 else if( !i_last_pixel && ( i_length >= 3 ) && ( i_length <= 9 ) )
2505 bs_write( s, 4, 0 );
2506 bs_write( s, 1, 0 );
2507 bs_write( s, 3, i_length - 2 ); /* (i_length - 2) * color 0 */
2509 else if( i_length > 2 )
2511 bs_write( s, 4, 0 );
2512 bs_write( s, 1, 1 );
2516 bs_write( s, 1, 0 );
2517 bs_write( s, 2, i_length - 4 );
2518 bs_write( s, 4, i_last_pixel );
2522 bs_write( s, 1, 1 );
2524 if( i_length <= 24 )
2526 bs_write( s, 2, 2 );
2527 bs_write( s, 4, i_length - 9 );
2528 bs_write( s, 4, i_last_pixel );
2532 bs_write( s, 2, 3 );
2533 bs_write( s, 8, i_length - 25 );
2534 bs_write( s, 4, i_last_pixel );
2539 if( i == p_region->fmt.i_visible_width ) break;
2541 i_last_pixel = p_data[i];
2546 bs_write( s, 8, 0 );
2552 static void encode_pixel_line_8bp( bs_t *s, subpicture_region_t *p_region,
2555 unsigned int i, i_length = 0;
2556 int i_pitch = p_region->p_picture->p->i_pitch;
2557 uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
2558 int i_last_pixel = p_data[0];
2560 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2562 if( ( i != p_region->fmt.i_visible_width ) &&
2563 ( p_data[i] == i_last_pixel ) && ( i_length != 127 ) )
2569 if( ( i_length == 1 ) && i_last_pixel )
2571 /* 8bit/pixel code */
2572 bs_write( s, 8, i_last_pixel );
2574 else if( ( i_length == 2 ) && i_last_pixel )
2576 /* 8bit/pixel code */
2577 bs_write( s, 8, i_last_pixel );
2578 bs_write( s, 8, i_last_pixel );
2580 else if( i_length <= 127 )
2582 bs_write( s, 8, 0 );
2586 bs_write( s, 1, 0 );
2587 bs_write( s, 7, i_length ); /* pseudo color 0 */
2591 bs_write( s, 1, 1 );
2592 bs_write( s, 7, i_length );
2593 bs_write( s, 8, i_last_pixel );
2597 if( i == p_region->fmt.i_visible_width ) break;
2599 i_last_pixel = p_data[i];
2604 bs_write( s, 8, 0 );
2605 bs_write( s, 8, 0 );