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-2004 VideoLAN
9 * Authors: Gildas Bazin <gbazin@videolan.org>
10 * Damien LUCAS <damien.lucas@anevia.com>
11 * Laurent Aimar <fenrir@via.ecp.fr>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
26 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
32 #include <vlc/decoder.h>
37 //#define DEBUG_DVBSUB 1
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
44 static subpicture_t *Decode( decoder_t *, block_t ** );
46 static int OpenEncoder ( vlc_object_t * );
47 static void CloseEncoder( vlc_object_t * );
48 static block_t *Encode ( encoder_t *, subpicture_t * );
51 set_description( _("DVB subtitles decoder") );
52 set_capability( "decoder", 50 );
53 set_category( CAT_INPUT );
54 set_subcategory( SUBCAT_INPUT_SCODEC );
55 set_callbacks( Open, Close );
57 # define ENC_CFG_PREFIX "sout-dvbsub-"
59 set_description( _("DVB subtitles encoder") );
60 set_capability( "encoder", 100 );
61 set_callbacks( OpenEncoder, CloseEncoder );
64 static const char *ppsz_enc_options[] = { NULL };
66 /****************************************************************************
68 ****************************************************************************
69 * Those structures refer closely to the ETSI 300 743 Object model
70 ****************************************************************************/
72 /* The object definition gives the position of the object in a region */
73 typedef struct dvbsub_objectdef_s
84 /* The entry in the palette CLUT */
95 typedef struct dvbsub_clut_s
99 dvbsub_color_t c_2b[4];
100 dvbsub_color_t c_4b[16];
101 dvbsub_color_t c_8b[256];
103 struct dvbsub_clut_s *p_next;
107 /* The Region is an aera on the image
108 * with a list of the object definitions associated and a CLUT */
109 typedef struct dvbsub_region_s
124 dvbsub_objectdef_t *p_object_defs;
126 struct dvbsub_region_s *p_next;
130 /* The object definition gives the position of the object in a region */
131 typedef struct dvbsub_regiondef_s
137 } dvbsub_regiondef_t;
139 /* The page defines the list of regions */
148 dvbsub_regiondef_t *p_region_defs;
156 /* Decoder internal data */
162 dvbsub_page_t *p_page;
163 dvbsub_region_t *p_regions;
164 dvbsub_clut_t *p_cluts;
165 dvbsub_clut_t default_clut;
169 // List of different SEGMENT TYPES
170 // According to EN 300-743, table 2
171 #define DVBSUB_ST_PAGE_COMPOSITION 0x10
172 #define DVBSUB_ST_REGION_COMPOSITION 0x11
173 #define DVBSUB_ST_CLUT_DEFINITION 0x12
174 #define DVBSUB_ST_OBJECT_DATA 0x13
175 #define DVBSUB_ST_ENDOFDISPLAY 0x80
176 #define DVBSUB_ST_STUFFING 0xff
177 // List of different OBJECT TYPES
178 // According to EN 300-743, table 6
179 #define DVBSUB_OT_BASIC_BITMAP 0x00
180 #define DVBSUB_OT_BASIC_CHAR 0x01
181 #define DVBSUB_OT_COMPOSITE_STRING 0x02
183 // According to EN 300-743, table 9
184 #define DVBSUB_DT_2BP_CODE_STRING 0x10
185 #define DVBSUB_DT_4BP_CODE_STRING 0x11
186 #define DVBSUB_DT_8BP_CODE_STRING 0x12
187 #define DVBSUB_DT_24_TABLE_DATA 0x20
188 #define DVBSUB_DT_28_TABLE_DATA 0x21
189 #define DVBSUB_DT_48_TABLE_DATA 0x22
190 #define DVBSUB_DT_END_LINE 0xf0
191 // List of different Page Composition Segment state
192 // According to EN 300-743, 7.2.1 table 3
193 #define DVBSUB_PCS_STATE_ACQUISITION 0x01
194 #define DVBSUB_PCS_STATE_CHANGE 0x10
196 /*****************************************************************************
198 *****************************************************************************/
199 static void decode_segment( decoder_t *, bs_t * );
200 static void decode_page_composition( decoder_t *, bs_t * );
201 static void decode_region_composition( decoder_t *, bs_t * );
202 static void decode_object( decoder_t *, bs_t * );
203 static void decode_clut( decoder_t *, bs_t * );
204 static void free_all( decoder_t * );
206 static void default_clut_init( decoder_t * );
208 static subpicture_t *render( decoder_t * );
210 /*****************************************************************************
211 * Open: probe the decoder and return score
212 *****************************************************************************
213 * Tries to launch a decoder and return score so that the interface is able
215 *****************************************************************************/
216 static int Open( vlc_object_t *p_this )
218 decoder_t *p_dec = (decoder_t *) p_this;
219 decoder_sys_t *p_sys;
221 if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','v','b','s') )
226 p_dec->pf_decode_sub = Decode;
227 p_sys = p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
228 memset( p_sys, 0, sizeof(decoder_sys_t) );
231 p_sys->i_id = p_dec->fmt_in.subs.dvb.i_id & 0xFFFF;
232 p_sys->i_ancillary_id = p_dec->fmt_in.subs.dvb.i_id >> 16;
234 p_sys->p_regions = NULL;
235 p_sys->p_cluts = NULL;
236 p_sys->p_page = NULL;
238 es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'd','v','b','s' ) );
240 default_clut_init( p_dec );
245 /*****************************************************************************
247 *****************************************************************************/
248 static void Close( vlc_object_t *p_this )
250 decoder_t *p_dec = (decoder_t*) p_this;
251 decoder_sys_t *p_sys = p_dec->p_sys;
257 /*****************************************************************************
259 *****************************************************************************/
260 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
262 decoder_sys_t *p_sys = p_dec->p_sys;
264 subpicture_t *p_spu = NULL;
266 if( pp_block == NULL || *pp_block == NULL ) return NULL;
270 p_sys->i_pts = p_block->i_pts;
271 if( p_sys->i_pts <= 0 )
274 /* Some DVB channels send stuffing segments in non-dated packets so
275 * don't complain too loudly. */
276 msg_Warn( p_dec, "non dated subtitle" );
278 block_Release( p_block );
282 bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
284 if( bs_read( &p_sys->bs, 8 ) != 0x20 ) /* Data identifier */
286 msg_Dbg( p_dec, "invalid data identifier" );
287 block_Release( p_block );
291 if( bs_read( &p_sys->bs, 8 ) ) /* Subtitle stream id */
293 msg_Dbg( p_dec, "invalid subtitle stream id" );
294 block_Release( p_block );
299 msg_Dbg( p_dec, "subtitle packet received: "I64Fd, p_sys->i_pts );
302 p_sys->b_page = VLC_FALSE;
303 while( bs_show( &p_sys->bs, 8 ) == 0x0f ) /* Sync byte */
305 decode_segment( p_dec, &p_sys->bs );
308 if( bs_read( &p_sys->bs, 8 ) != 0xff ) /* End marker */
310 msg_Warn( p_dec, "end marker not found (corrupted subtitle ?)" );
311 block_Release( p_block );
315 /* Check if the page is to be displayed */
316 if( p_sys->p_page && p_sys->b_page ) p_spu = render( p_dec );
318 block_Release( p_block );
323 /* following functions are local */
325 /*****************************************************************************
326 * default_clut_init: default clut as defined in EN 300-743 section 10
327 *****************************************************************************/
328 static void default_clut_init( decoder_t *p_dec )
330 decoder_sys_t *p_sys = p_dec->p_sys;
333 #define RGB_TO_Y(r, g, b) ((int16_t) 77 * r + 150 * g + 29 * b) / 256;
334 #define RGB_TO_U(r, g, b) ((int16_t) -44 * r - 87 * g + 131 * b) / 256;
335 #define RGB_TO_V(r, g, b) ((int16_t) 131 * r - 110 * g - 21 * b) / 256;
338 for( i = 0; i < 4; i++ )
340 uint8_t R = 0, G = 0, B = 0, T = 0;
342 if( !(i & 0x2) && !(i & 0x1) ) T = 0xFF;
343 else if( !(i & 0x2) && (i & 0x1) ) R = G = B = 0xFF;
344 else if( (i & 0x2) && !(i & 0x1) ) R = G = B = 0;
345 else R = G = B = 0x7F;
347 p_sys->default_clut.c_2b[i].Y = RGB_TO_Y(R,G,B);
348 p_sys->default_clut.c_2b[i].Cr = RGB_TO_U(R,G,B);
349 p_sys->default_clut.c_2b[i].Cb = RGB_TO_V(R,G,B);
350 p_sys->default_clut.c_2b[i].T = T;
353 /* 16 entries CLUT */
354 for( i = 0; i < 16; i++ )
356 uint8_t R = 0, G = 0, B = 0, T = 0;
360 if( !(i & 0x4) && !(i & 0x2) && !(i & 0x1) )
366 R = (i & 0x1) ? 0xFF : 0;
367 G = (i & 0x2) ? 0xFF : 0;
368 B = (i & 0x4) ? 0xFF : 0;
373 R = (i & 0x1) ? 0x7F : 0;
374 G = (i & 0x2) ? 0x7F : 0;
375 B = (i & 0x4) ? 0x7F : 0;
378 p_sys->default_clut.c_4b[i].Y = RGB_TO_Y(R,G,B);
379 p_sys->default_clut.c_4b[i].Cr = RGB_TO_U(R,G,B);
380 p_sys->default_clut.c_4b[i].Cb = RGB_TO_V(R,G,B);
381 p_sys->default_clut.c_4b[i].T = T;
384 /* 256 entries CLUT (TODO) */
385 memset( p_sys->default_clut.c_8b, 0xFF, 256 * sizeof(dvbsub_color_t) );
388 static void decode_segment( decoder_t *p_dec, bs_t *s )
390 decoder_sys_t *p_sys = p_dec->p_sys;
395 /* sync_byte (already checked) */
399 i_type = bs_read( s, 8 );
402 i_page_id = bs_read( s, 16 );
405 i_size = bs_show( s, 16 );
407 if( i_page_id != p_sys->i_id && i_page_id != p_sys->i_ancillary_id )
410 msg_Dbg( p_dec, "subtitle skipped (page id: %i, %i)",
411 i_page_id, p_sys->i_id );
413 bs_skip( s, 8 * ( 2 + i_size ) );
417 if( p_sys->i_ancillary_id != p_sys->i_id &&
418 i_type == DVBSUB_ST_PAGE_COMPOSITION &&
419 i_page_id == p_sys->i_ancillary_id )
422 msg_Dbg( p_dec, "skipped invalid ancillary subtitle packet" );
424 bs_skip( s, 8 * ( 2 + i_size ) );
429 if( i_page_id == p_sys->i_id )
430 msg_Dbg( p_dec, "segment (id: %i)", i_page_id );
432 msg_Dbg( p_dec, "ancillary segment (id: %i)", i_page_id );
437 case DVBSUB_ST_PAGE_COMPOSITION:
439 msg_Dbg( p_dec, "decode_page_composition" );
441 decode_page_composition( p_dec, s );
444 case DVBSUB_ST_REGION_COMPOSITION:
446 msg_Dbg( p_dec, "decode_region_composition" );
448 decode_region_composition( p_dec, s );
451 case DVBSUB_ST_CLUT_DEFINITION:
453 msg_Dbg( p_dec, "decode_clut" );
455 decode_clut( p_dec, s );
458 case DVBSUB_ST_OBJECT_DATA:
460 msg_Dbg( p_dec, "decode_object" );
462 decode_object( p_dec, s );
465 case DVBSUB_ST_ENDOFDISPLAY:
467 msg_Dbg( p_dec, "end of display" );
469 bs_skip( s, 8 * ( 2 + i_size ) );
472 case DVBSUB_ST_STUFFING:
474 msg_Dbg( p_dec, "skip stuffing" );
476 bs_skip( s, 8 * ( 2 + i_size ) );
480 msg_Warn( p_dec, "unsupported segment type: (%04x)", i_type );
481 bs_skip( s, 8 * ( 2 + i_size ) );
486 static void decode_clut( decoder_t *p_dec, bs_t *s )
488 decoder_sys_t *p_sys = p_dec->p_sys;
489 uint16_t i_segment_length;
490 uint16_t i_processed_length;
491 dvbsub_clut_t *p_clut, *p_next;
494 i_segment_length = bs_read( s, 16 );
495 i_id = bs_read( s, 8 );
496 i_version = bs_read( s, 4 );
498 /* Check if we already have this clut */
499 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next )
501 if( p_clut->i_id == i_id ) break;
504 /* Check version number */
505 if( p_clut && p_clut->i_version == i_version )
508 bs_skip( s, 8 * i_segment_length - 12 );
515 msg_Dbg( p_dec, "new clut: %i", i_id );
517 p_clut = malloc( sizeof(dvbsub_clut_t) );
518 p_clut->p_next = p_sys->p_cluts;
519 p_sys->p_cluts = p_clut;
522 /* Initialize to default clut */
523 p_next = p_clut->p_next;
524 *p_clut = p_sys->default_clut;
525 p_clut->p_next = p_next;
527 /* We don't have this version of the CLUT: Parse it */
528 p_clut->i_version = i_version;
530 bs_skip( s, 4 ); /* Reserved bits */
531 i_processed_length = 2;
532 while( i_processed_length < i_segment_length )
534 uint8_t y, cb, cr, t;
538 i_id = bs_read( s, 8 );
539 i_type = bs_read( s, 3 );
543 if( bs_read( s, 1 ) )
546 cr = bs_read( s, 8 );
547 cb = bs_read( s, 8 );
549 i_processed_length += 6;
553 y = bs_read( s, 6 ) << 2;
554 cr = bs_read( s, 4 ) << 4;
555 cb = bs_read( s, 4 ) << 4;
556 t = bs_read( s, 2 ) << 6;
557 i_processed_length += 4;
560 /* We are not entirely compliant here as full transparency is indicated
561 * with a luma value of zero, not a transparency value of 0xff
562 * (full transparency would actually be 0xff + 1). */
570 /* According to EN 300-743 section 7.2.3 note 1, type should
571 * not have more than 1 bit set to one, but some streams don't
572 * respect this note. */
574 if( i_type & 0x04 && i_id < 4 )
576 p_clut->c_2b[i_id].Y = y;
577 p_clut->c_2b[i_id].Cr = cr;
578 p_clut->c_2b[i_id].Cb = cb;
579 p_clut->c_2b[i_id].T = t;
581 if( i_type & 0x02 && i_id < 16 )
583 p_clut->c_4b[i_id].Y = y;
584 p_clut->c_4b[i_id].Cr = cr;
585 p_clut->c_4b[i_id].Cb = cb;
586 p_clut->c_4b[i_id].T = t;
590 p_clut->c_8b[i_id].Y = y;
591 p_clut->c_8b[i_id].Cr = cr;
592 p_clut->c_8b[i_id].Cb = cb;
593 p_clut->c_8b[i_id].T = t;
598 static void decode_page_composition( decoder_t *p_dec, bs_t *s )
600 decoder_sys_t *p_sys = p_dec->p_sys;
601 int i_version, i_state, i_segment_length, i_timeout, i;
603 /* A page is composed by 0 or more region */
605 i_segment_length = bs_read( s, 16 );
606 i_timeout = bs_read( s, 8 );
607 i_version = bs_read( s, 4 );
608 i_state = bs_read( s, 2 );
609 bs_skip( s, 2 ); /* Reserved */
611 if( i_state == DVBSUB_PCS_STATE_CHANGE )
613 /* End of an epoch, reset decoder buffer */
615 msg_Dbg( p_dec, "page composition mode change" );
619 else if( !p_sys->p_page && i_state != DVBSUB_PCS_STATE_ACQUISITION &&
620 i_state != DVBSUB_PCS_STATE_CHANGE )
622 /* Not a full PCS, we need to wait for one */
623 msg_Dbg( p_dec, "didn't receive an acquisition page yet" );
625 #if 0 /* Try to start decoding even without an acquisition page */
626 bs_skip( s, 8 * (i_segment_length - 2) );
632 if( i_state == DVBSUB_PCS_STATE_ACQUISITION )
633 msg_Dbg( p_dec, "acquisition page composition" );
636 /* Check version number */
637 if( p_sys->p_page && p_sys->p_page->i_version == i_version )
639 bs_skip( s, 8 * (i_segment_length - 2) );
642 else if( p_sys->p_page )
644 if( p_sys->p_page->i_region_defs )
645 free( p_sys->p_page->p_region_defs );
646 p_sys->p_page->i_region_defs = 0;
652 msg_Dbg( p_dec, "new page" );
654 /* Allocate a new page */
655 p_sys->p_page = malloc( sizeof(dvbsub_page_t) );
658 p_sys->p_page->i_version = i_version;
659 p_sys->p_page->i_timeout = i_timeout;
660 p_sys->b_page = VLC_TRUE;
662 /* Number of regions */
663 p_sys->p_page->i_region_defs = (i_segment_length - 2) / 6;
665 if( p_sys->p_page->i_region_defs == 0 ) return;
667 p_sys->p_page->p_region_defs =
668 malloc( p_sys->p_page->i_region_defs * sizeof(dvbsub_region_t) );
669 for( i = 0; i < p_sys->p_page->i_region_defs; i++ )
671 p_sys->p_page->p_region_defs[i].i_id = bs_read( s, 8 );
672 bs_skip( s, 8 ); /* Reserved */
673 p_sys->p_page->p_region_defs[i].i_x = bs_read( s, 16 );
674 p_sys->p_page->p_region_defs[i].i_y = bs_read( s, 16 );
677 msg_Dbg( p_dec, "page_composition, region %i (%i,%i)",
678 i, p_sys->p_page->p_region_defs[i].i_x,
679 p_sys->p_page->p_region_defs[i].i_y );
684 static void decode_region_composition( decoder_t *p_dec, bs_t *s )
686 decoder_sys_t *p_sys = p_dec->p_sys;
687 dvbsub_region_t *p_region, **pp_region = &p_sys->p_regions;
688 int i_segment_length, i_processed_length, i_id, i_version;
689 int i_width, i_height, i_level_comp, i_depth, i_clut;
690 int i_8_bg, i_4_bg, i_2_bg;
693 i_segment_length = bs_read( s, 16 );
694 i_id = bs_read( s, 8 );
695 i_version = bs_read( s, 4 );
697 /* Check if we already have this region */
698 for( p_region = p_sys->p_regions; p_region != NULL;
699 p_region = p_region->p_next )
701 pp_region = &p_region->p_next;
702 if( p_region->i_id == i_id ) break;
705 /* Check version number */
706 if( p_region && p_region->i_version == i_version )
708 bs_skip( s, 8 * (i_segment_length - 1) - 4 );
715 msg_Dbg( p_dec, "new region: %i", i_id );
717 p_region = *pp_region = malloc( sizeof(dvbsub_region_t) );
718 memset( p_region, 0, sizeof(dvbsub_region_t) );
719 p_region->p_object_defs = NULL;
720 p_region->p_pixbuf = NULL;
721 p_region->p_next = NULL;
724 /* Region attributes */
725 p_region->i_id = i_id;
726 p_region->i_version = i_version;
727 b_fill = bs_read( s, 1 );
728 bs_skip( s, 3 ); /* Reserved */
730 i_width = bs_read( s, 16 );
731 i_height = bs_read( s, 16 );
732 i_level_comp = bs_read( s, 3 );
733 i_depth = bs_read( s, 3 );
734 bs_skip( s, 2 ); /* Reserved */
735 i_clut = bs_read( s, 8 );
737 i_8_bg = bs_read( s, 8 );
738 i_4_bg = bs_read( s, 4 );
739 i_2_bg = bs_read( s, 2 );
740 bs_skip( s, 2 ); /* Reserved */
741 p_region->i_object_defs = 0;
743 /* Extra sanity checks */
744 if( p_region->i_width != i_width || p_region->i_height != i_height )
746 if( p_region->p_pixbuf )
748 msg_Dbg( p_dec, "region size changed (not allowed)" );
749 free( p_region->p_pixbuf );
752 p_region->p_pixbuf = malloc( i_height * i_width );
753 p_region->i_depth = 0;
756 if( p_region->i_depth && (p_region->i_depth != i_depth ||
757 p_region->i_level_comp != i_level_comp || p_region->i_clut != i_clut) )
759 msg_Dbg( p_dec, "region parameters changed (not allowed)" );
762 /* Erase background of region */
765 int i_background = (p_region->i_depth == 1) ? i_2_bg :
766 (p_region->i_depth == 2) ? i_4_bg : i_8_bg;
767 memset( p_region->p_pixbuf, i_background, i_width * i_height );
770 p_region->i_width = i_width;
771 p_region->i_height = i_height;
772 p_region->i_level_comp = i_level_comp;
773 p_region->i_depth = i_depth;
774 p_region->i_clut = i_clut;
776 /* List of objects in the region */
777 i_processed_length = 10;
778 while( i_processed_length < i_segment_length )
780 dvbsub_objectdef_t *p_obj;
782 /* We create a new object */
783 p_region->i_object_defs++;
784 p_region->p_object_defs =
785 realloc( p_region->p_object_defs,
786 sizeof(dvbsub_objectdef_t) * p_region->i_object_defs );
788 /* We parse object properties */
789 p_obj = &p_region->p_object_defs[p_region->i_object_defs - 1];
790 p_obj->i_id = bs_read( s, 16 );
791 p_obj->i_type = bs_read( s, 2 );
792 bs_skip( s, 2 ); /* Provider */
793 p_obj->i_x = bs_read( s, 12 );
794 bs_skip( s, 4 ); /* Reserved */
795 p_obj->i_y = bs_read( s, 12 );
797 i_processed_length += 6;
799 if( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ||
800 p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING )
802 p_obj->i_fg_pc = bs_read( s, 8 );
803 p_obj->i_bg_pc = bs_read( s, 8 );
804 i_processed_length += 2;
809 static void dvbsub_render_pdata( decoder_t *, dvbsub_region_t *, int, int,
811 static void dvbsub_pdata2bpp( bs_t *, uint8_t *, int, int * );
812 static void dvbsub_pdata4bpp( bs_t *, uint8_t *, int, int * );
813 static void dvbsub_pdata8bpp( bs_t *, uint8_t *, int, int * );
815 static void decode_object( decoder_t *p_dec, bs_t *s )
817 decoder_sys_t *p_sys = p_dec->p_sys;
818 dvbsub_region_t *p_region;
819 int i_segment_length, i_coding_method, i_version, i_id, i;
820 vlc_bool_t b_non_modify_color;
822 i_segment_length = bs_read( s, 16 );
823 i_id = bs_read( s, 16 );
824 i_version = bs_read( s, 4 );
825 i_coding_method = bs_read( s, 2 );
827 if( i_coding_method )
829 /* TODO: DVB subtitling as characters */
830 msg_Dbg( p_dec, "DVB subtitling as characters is not handled!" );
831 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
835 /* Check if the object needs to be rendered in at least one
837 for( p_region = p_sys->p_regions; p_region != NULL;
838 p_region = p_region->p_next )
840 for( i = 0; i < p_region->i_object_defs; i++ )
841 if( p_region->p_object_defs[i].i_id == i_id ) break;
843 if( i != p_region->i_object_defs ) break;
847 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
852 msg_Dbg( p_dec, "new object: %i", i_id );
855 b_non_modify_color = bs_read( s, 1 );
856 bs_skip( s, 1 ); /* Reserved */
858 if( i_coding_method == 0x00 )
860 int i_topfield, i_bottomfield;
861 uint8_t *p_topfield, *p_bottomfield;
863 i_topfield = bs_read( s, 16 );
864 i_bottomfield = bs_read( s, 16 );
865 p_topfield = s->p_start + bs_pos( s ) / 8;
866 p_bottomfield = p_topfield + i_topfield;
868 bs_skip( s, 8 * (i_segment_length - 7) );
871 if( i_segment_length < i_topfield + i_bottomfield + 7 ||
872 p_topfield + i_topfield + i_bottomfield > s->p_end )
874 msg_Dbg( p_dec, "corrupted object data" );
878 for( p_region = p_sys->p_regions; p_region != NULL;
879 p_region = p_region->p_next )
881 for( i = 0; i < p_region->i_object_defs; i++ )
883 if( p_region->p_object_defs[i].i_id != i_id ) continue;
885 dvbsub_render_pdata( p_dec, p_region,
886 p_region->p_object_defs[i].i_x,
887 p_region->p_object_defs[i].i_y,
888 p_topfield, i_topfield );
892 dvbsub_render_pdata( p_dec, p_region,
893 p_region->p_object_defs[i].i_x,
894 p_region->p_object_defs[i].i_y + 1,
895 p_bottomfield, i_bottomfield );
899 /* Duplicate the top field */
900 dvbsub_render_pdata( p_dec, p_region,
901 p_region->p_object_defs[i].i_x,
902 p_region->p_object_defs[i].i_y + 1,
903 p_topfield, i_topfield );
910 /* TODO: DVB subtitling as characters */
914 msg_Dbg( p_dec, "end object: %i", i_id );
918 static void dvbsub_render_pdata( decoder_t *p_dec, dvbsub_region_t *p_region,
920 uint8_t *p_field, int i_field )
927 if( !p_region->p_pixbuf )
929 msg_Err( p_dec, "region %i has no pixel buffer!", p_region->i_id );
932 if( i_y < 0 || i_x < 0 || i_y >= p_region->i_height ||
933 i_x >= p_region->i_width )
935 msg_Dbg( p_dec, "invalid offset (%i,%i)", i_x, i_y );
939 p_pixbuf = p_region->p_pixbuf + i_y * p_region->i_width;
940 bs_init( &bs, p_field, i_field );
942 while( !bs_eof( &bs ) )
945 if( i_y >= p_region->i_height ) return;
947 switch( bs_read( &bs, 8 ) )
950 dvbsub_pdata2bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
955 dvbsub_pdata4bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
960 dvbsub_pdata8bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
967 /* We don't use map tables */
970 case 0xf0: /* End of line code */
971 p_pixbuf += 2*p_region->i_width;
972 i_offset = 0; i_y += 2;
978 static void dvbsub_pdata2bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
980 vlc_bool_t b_stop = 0;
982 while( !b_stop && !bs_eof( s ) )
984 int i_count = 0, i_color = 0;
986 if( (i_color = bs_read( s, 2 )) != 0x00 )
992 if( bs_read( s, 1 ) == 0x01 ) // Switch1
994 i_count = 3 + bs_read( s, 3 );
995 i_color = bs_read( s, 2 );
999 if( bs_read( s, 1 ) == 0x00 ) //Switch2
1001 switch( bs_read( s, 2 ) ) //Switch3
1010 i_count = 12 + bs_read( s, 4 );
1011 i_color = bs_read( s, 2 );
1014 i_count = 29 + bs_read( s, 8 );
1015 i_color = bs_read( s, 2 );
1023 /* 1 pixel color 0 */
1029 if( !i_count ) continue;
1032 if( i_count + *pi_off > i_width ) break;
1034 if( i_count == 1 ) p[*pi_off] = i_color;
1035 else memset( p + *pi_off, i_color, i_count );
1037 (*pi_off) += i_count;
1043 static void dvbsub_pdata4bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1045 vlc_bool_t b_stop = 0;
1047 while( !b_stop && !bs_eof( s ) )
1049 int i_count = 0, i_color = 0;
1051 if( (i_color = bs_read( s, 4 )) != 0x00 )
1058 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1060 if( bs_show( s, 3 ) != 0x00 )
1062 i_count = 2 + bs_read( s, 3 );
1072 if( bs_read( s, 1 ) == 0x00) //Switch2
1074 i_count = 4 + bs_read( s, 2 );
1075 i_color = bs_read( s, 4 );
1079 switch ( bs_read( s, 2 ) ) //Switch3
1088 i_count = 9 + bs_read( s, 4 );
1089 i_color = bs_read( s, 4 );
1092 i_count= 25 + bs_read( s, 8 );
1093 i_color = bs_read( s, 4 );
1100 if( !i_count ) continue;
1103 if( i_count + *pi_off > i_width ) break;
1105 if( i_count == 1 ) p[*pi_off] = i_color;
1106 else memset( p + *pi_off, i_color, i_count );
1108 (*pi_off) += i_count;
1114 static void dvbsub_pdata8bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1116 vlc_bool_t b_stop = 0;
1118 while( !b_stop && !bs_eof( s ) )
1120 int i_count = 0, i_color = 0;
1122 if( (i_color = bs_read( s, 8 )) != 0x00 )
1129 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1131 if( bs_show( s, 7 ) != 0x00 )
1133 i_count = bs_read( s, 7 );
1143 i_count = bs_read( s, 7 );
1144 i_color = bs_read( s, 8 );
1148 if( !i_count ) continue;
1151 if( i_count + *pi_off > i_width ) break;
1153 if( i_count == 1 ) p[*pi_off] = i_color;
1154 else memset( p + *pi_off, i_color, i_count );
1156 (*pi_off) += i_count;
1162 static void free_all( decoder_t *p_dec )
1164 decoder_sys_t *p_sys = p_dec->p_sys;
1165 dvbsub_region_t *p_reg, *p_reg_next;
1166 dvbsub_clut_t *p_clut, *p_clut_next;
1168 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut_next )
1170 p_clut_next = p_clut->p_next;
1173 p_sys->p_cluts = NULL;
1175 for( p_reg = p_sys->p_regions; p_reg != NULL; p_reg = p_reg_next )
1177 p_reg_next = p_reg->p_next;
1178 if( p_reg->i_object_defs ) free( p_reg->p_object_defs );
1179 if( p_reg->p_pixbuf ) free( p_reg->p_pixbuf );
1182 p_sys->p_regions = NULL;
1186 if( p_sys->p_page->i_region_defs )
1187 free( p_sys->p_page->p_region_defs );
1188 free( p_sys->p_page );
1190 p_sys->p_page = NULL;
1193 static subpicture_t *render( decoder_t *p_dec )
1195 decoder_sys_t *p_sys = p_dec->p_sys;
1196 subpicture_t *p_spu;
1197 subpicture_region_t **pp_spu_region;
1198 int i, j, i_timeout = 0;
1200 /* Allocate the subpicture internal data. */
1201 p_spu = p_dec->pf_spu_buffer_new( p_dec );
1202 if( !p_spu ) return NULL;
1204 pp_spu_region = &p_spu->p_region;
1206 /* Loop on region definitions */
1209 msg_Dbg( p_dec, "rendering %i regions", p_sys->p_page->i_region_defs );
1212 for( i = 0; p_sys->p_page && i < p_sys->p_page->i_region_defs; i++ )
1214 dvbsub_region_t *p_region;
1215 dvbsub_regiondef_t *p_regiondef;
1216 dvbsub_clut_t *p_clut;
1217 dvbsub_color_t *p_color;
1218 subpicture_region_t *p_spu_region;
1219 uint8_t *p_src, *p_dst;
1223 i_timeout = p_sys->p_page->i_timeout;
1225 p_regiondef = &p_sys->p_page->p_region_defs[i];
1228 msg_Dbg( p_dec, "rendering region %i (%i,%i)", i,
1229 p_regiondef->i_x, p_regiondef->i_y );
1232 /* Find associated region */
1233 for( p_region = p_sys->p_regions; p_region != NULL;
1234 p_region = p_region->p_next )
1236 if( p_regiondef->i_id == p_region->i_id ) break;
1241 msg_Dbg( p_dec, "region %i not found", p_regiondef->i_id );
1245 /* Find associated CLUT */
1246 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next )
1248 if( p_region->i_clut == p_clut->i_id ) break;
1252 msg_Dbg( p_dec, "clut %i not found", p_region->i_clut );
1256 /* Create new SPU region */
1257 memset( &fmt, 0, sizeof(video_format_t) );
1258 fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
1259 fmt.i_aspect = VOUT_ASPECT_FACTOR;
1260 fmt.i_width = fmt.i_visible_width = p_region->i_width;
1261 fmt.i_height = fmt.i_visible_height = p_region->i_height;
1262 fmt.i_x_offset = fmt.i_y_offset = 0;
1263 p_spu_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
1266 msg_Err( p_dec, "cannot allocate SPU region" );
1269 p_spu_region->i_x = p_regiondef->i_x;
1270 p_spu_region->i_y = p_regiondef->i_y;
1271 *pp_spu_region = p_spu_region;
1272 pp_spu_region = &p_spu_region->p_next;
1275 fmt.p_palette->i_entries = p_region->i_depth == 1 ? 4 :
1276 p_region->i_depth == 2 ? 16 : 256;
1277 p_color = (p_region->i_depth == 1) ? p_clut->c_2b :
1278 (p_region->i_depth == 2) ? p_clut->c_4b : p_clut->c_8b;
1279 for( j = 0; j < fmt.p_palette->i_entries; j++ )
1281 fmt.p_palette->palette[j][0] = p_color[j].Y;
1282 fmt.p_palette->palette[j][1] = p_color[j].Cr;
1283 fmt.p_palette->palette[j][2] = p_color[j].Cb;
1284 fmt.p_palette->palette[j][3] = 0xff - p_color[j].T;
1287 p_src = p_region->p_pixbuf;
1288 p_dst = p_spu_region->picture.Y_PIXELS;
1289 i_pitch = p_spu_region->picture.Y_PITCH;
1291 /* Copy pixel buffer */
1292 for( j = 0; j < p_region->i_height; j++ )
1294 memcpy( p_dst, p_src, p_region->i_width );
1295 p_src += p_region->i_width;
1300 /* Set the pf_render callback */
1301 p_spu->i_start = p_sys->i_pts;
1302 p_spu->i_stop = p_spu->i_start + i_timeout * 1000000;
1303 p_spu->b_ephemer = VLC_TRUE;
1308 /*****************************************************************************
1309 * encoder_sys_t : encoder descriptor
1310 *****************************************************************************/
1311 typedef struct encoder_region_t
1318 struct encoder_sys_t
1320 unsigned int i_page_ver;
1321 unsigned int i_region_ver;
1322 unsigned int i_clut_ver;
1325 encoder_region_t *p_regions;
1330 static void encode_page_composition( encoder_t *, bs_t *, subpicture_t * );
1331 static void encode_clut( encoder_t *, bs_t *, subpicture_t * );
1332 static void encode_region_composition( encoder_t *, bs_t *, subpicture_t * );
1333 static void encode_object( encoder_t *, bs_t *, subpicture_t * );
1335 /*****************************************************************************
1336 * OpenEncoder: probe the encoder and return score
1337 *****************************************************************************/
1338 static int OpenEncoder( vlc_object_t *p_this )
1340 encoder_t *p_enc = (encoder_t *)p_this;
1341 encoder_sys_t *p_sys;
1343 if( p_enc->fmt_out.i_codec != VLC_FOURCC('d','v','b','s') &&
1346 return VLC_EGENERIC;
1349 /* Allocate the memory needed to store the decoder's structure */
1350 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1352 msg_Err( p_enc, "out of memory" );
1353 return VLC_EGENERIC;
1355 p_enc->p_sys = p_sys;
1357 p_enc->pf_encode_sub = Encode;
1358 p_enc->fmt_out.i_codec = VLC_FOURCC('d','v','b','s');
1359 p_enc->fmt_out.subs.dvb.i_id = 1 << 16 | 1;
1361 sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
1363 p_sys->i_page_ver = 0;
1364 p_sys->i_region_ver = 0;
1365 p_sys->i_clut_ver = 0;
1366 p_sys->i_regions = 0;
1367 p_sys->p_regions = 0;
1372 /****************************************************************************
1373 * Encode: the whole thing
1374 ****************************************************************************/
1375 static block_t *Encode( encoder_t *p_enc, subpicture_t *p_subpic )
1377 bs_t bits, *s = &bits;
1380 if( !p_subpic || !p_subpic->p_region ) return 0;
1382 msg_Dbg( p_enc, "encoding subpicture" );
1384 p_block = block_New( p_enc, 64000 );
1385 bs_init( s, p_block->p_buffer, p_block->i_buffer );
1387 bs_write( s, 8, 0x20 ); /* Data identifier */
1388 bs_write( s, 8, 0x0 ); /* Subtitle stream id */
1390 encode_page_composition( p_enc, s, p_subpic );
1391 encode_region_composition( p_enc, s, p_subpic );
1392 encode_clut( p_enc, s, p_subpic );
1393 encode_object( p_enc, s, p_subpic );
1395 /* End of display */
1396 bs_write( s, 8, 0x0f ); /* Sync byte */
1397 bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
1398 bs_write( s, 16, 1 ); /* Page id */
1399 bs_write( s, 16, 0 ); /* Segment length */
1401 bs_write( s, 8, 0xff ); /* End marker */
1402 p_block->i_buffer = bs_pos( s ) / 8;
1403 p_block->i_pts = p_block->i_dts = p_subpic->i_start;
1404 if( !p_subpic->b_ephemer && p_subpic->i_stop > p_subpic->i_start )
1406 block_t *p_block_stop;
1408 p_block->i_length = p_subpic->i_stop - p_subpic->i_start;
1410 /* Send another (empty) subtitle to signal the end of display */
1411 p_block_stop = block_New( p_enc, 64000 );
1412 bs_init( s, p_block_stop->p_buffer, p_block_stop->i_buffer );
1413 bs_write( s, 8, 0x20 ); /* Data identifier */
1414 bs_write( s, 8, 0x0 ); /* Subtitle stream id */
1415 encode_page_composition( p_enc, s, 0 );
1416 bs_write( s, 8, 0x0f ); /* Sync byte */
1417 bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
1418 bs_write( s, 16, 1 ); /* Page id */
1419 bs_write( s, 16, 0 ); /* Segment length */
1420 bs_write( s, 8, 0xff ); /* End marker */
1421 p_block_stop->i_buffer = bs_pos( s ) / 8;
1422 p_block_stop->i_pts = p_block_stop->i_dts = p_subpic->i_stop;
1423 block_ChainAppend( &p_block, p_block_stop );
1424 p_block_stop->i_length = 100000;//p_subpic->i_stop - p_subpic->i_start;
1427 msg_Dbg( p_enc, "subpicture encoded properly" );
1432 /*****************************************************************************
1433 * CloseEncoder: encoder destruction
1434 *****************************************************************************/
1435 static void CloseEncoder( vlc_object_t *p_this )
1437 encoder_t *p_enc = (encoder_t *)p_this;
1438 encoder_sys_t *p_sys = p_enc->p_sys;
1440 if( p_sys->i_regions ) free( p_sys->p_regions );
1444 static void encode_page_composition( encoder_t *p_enc, bs_t *s,
1445 subpicture_t *p_subpic )
1447 encoder_sys_t *p_sys = p_enc->p_sys;
1448 subpicture_region_t *p_region;
1449 vlc_bool_t b_mode_change = VLC_FALSE;
1450 int i_regions, i_timeout;
1452 bs_write( s, 8, 0x0f ); /* Sync byte */
1453 bs_write( s, 8, DVBSUB_ST_PAGE_COMPOSITION ); /* Segment type */
1454 bs_write( s, 16, 1 ); /* Page id */
1456 for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
1457 p_region; p_region = p_region->p_next, i_regions++ )
1459 if( i_regions >= p_sys->i_regions )
1461 encoder_region_t region;
1462 region.i_width = region.i_height = 0;
1464 realloc( p_sys->p_regions, sizeof(encoder_region_t) *
1465 (p_sys->i_regions + 1) );
1466 p_sys->p_regions[p_sys->i_regions++] = region;
1469 if( p_sys->p_regions[i_regions].i_width <
1470 (int)p_region->fmt.i_visible_width )
1472 b_mode_change = VLC_TRUE;
1473 msg_Dbg( p_enc, "region %i width change: %i -> %i",
1474 i_regions, p_sys->p_regions[i_regions].i_width,
1475 p_region->fmt.i_visible_width );
1476 p_sys->p_regions[i_regions].i_width =
1477 p_region->fmt.i_visible_width;
1479 if( p_sys->p_regions[i_regions].i_height <
1480 (int)p_region->fmt.i_visible_height )
1482 b_mode_change = VLC_TRUE;
1483 msg_Dbg( p_enc, "region %i height change: %i -> %i",
1484 i_regions, p_sys->p_regions[i_regions].i_height,
1485 p_region->fmt.i_visible_height );
1486 p_sys->p_regions[i_regions].i_height =
1487 p_region->fmt.i_visible_height;
1491 bs_write( s, 16, i_regions * 6 + 2 ); /* Segment length */
1494 if( p_subpic && !p_subpic->b_ephemer &&
1495 p_subpic->i_stop > p_subpic->i_start )
1497 i_timeout = (p_subpic->i_stop - p_subpic->i_start) / 1000000;
1500 bs_write( s, 8, i_timeout + 15 ); /* Timeout */
1501 bs_write( s, 4, p_sys->i_page_ver++ );
1502 bs_write( s, 2, b_mode_change ?
1503 DVBSUB_PCS_STATE_CHANGE : DVBSUB_PCS_STATE_ACQUISITION );
1504 bs_write( s, 2, 0 ); /* Reserved */
1506 for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
1507 p_region; p_region = p_region->p_next, i_regions++ )
1509 bs_write( s, 8, i_regions );
1510 bs_write( s, 8, 0 ); /* Reserved */
1511 bs_write( s, 16, p_region->i_x );
1512 bs_write( s, 16, p_region->i_y );
1516 static void encode_clut( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
1518 encoder_sys_t *p_sys = p_enc->p_sys;
1519 subpicture_region_t *p_region = p_subpic->p_region;
1520 video_palette_t *p_pal;
1524 if( !p_region || !p_region->fmt.p_palette ||
1525 p_region->fmt.i_chroma != VLC_FOURCC('Y','U','V','P') ) return;
1527 bs_write( s, 8, 0x0f ); /* Sync byte */
1528 bs_write( s, 8, DVBSUB_ST_CLUT_DEFINITION ); /* Segment type */
1529 bs_write( s, 16, 1 ); /* Page id */
1531 p_pal = p_region->fmt.p_palette;
1533 bs_write( s, 16, p_pal->i_entries * 6 + 2 ); /* Segment length */
1534 bs_write( s, 8, 1 ); /* Clut id */
1535 bs_write( s, 4, p_sys->i_clut_ver++ );
1536 bs_write( s, 4, 0 ); /* Reserved */
1538 for( i = 0; i < p_pal->i_entries; i++ )
1540 bs_write( s, 8, i ); /* Clut entry id */
1541 bs_write( s, 1, p_pal->i_entries == 4 ); /* 2bit/entry flag */
1542 bs_write( s, 1, p_pal->i_entries == 16 ); /* 4bit/entry flag */
1543 bs_write( s, 1, p_pal->i_entries == 256 ); /* 8bit/entry flag */
1544 bs_write( s, 4, 0 ); /* Reserved */
1545 bs_write( s, 1, 1 ); /* Full range flag */
1546 bs_write( s, 8, p_pal->palette[i][3] ? /* Y value */
1547 (p_pal->palette[i][0] ? p_pal->palette[i][0] : 16) : 0 );
1548 bs_write( s, 8, p_pal->palette[i][1] ); /* Cr value */
1549 bs_write( s, 8, p_pal->palette[i][2] ); /* Cb value */
1550 bs_write( s, 8, 0xff - p_pal->palette[i][3] ); /* T value */
1554 static void encode_region_composition( encoder_t *p_enc, bs_t *s,
1555 subpicture_t *p_subpic )
1557 encoder_sys_t *p_sys = p_enc->p_sys;
1558 subpicture_region_t *p_region;
1561 for( i_region = 0, p_region = p_subpic->p_region; p_region;
1562 p_region = p_region->p_next, i_region++ )
1564 video_palette_t *p_pal = p_region->fmt.p_palette;
1565 int i_depth = p_pal->i_entries == 4 ? 0x1 :
1566 p_pal->i_entries == 16 ? 0x2 : 0x3;
1568 for( i_bg = 0; i_bg < p_pal->i_entries; i_bg++ )
1570 if( !p_pal->palette[i_bg][3] ) break;
1573 bs_write( s, 8, 0x0f ); /* Sync byte */
1574 bs_write( s, 8, DVBSUB_ST_REGION_COMPOSITION ); /* Segment type */
1575 bs_write( s, 16, 1 ); /* Page id */
1577 bs_write( s, 16, 10 + 6 ); /* Segment length */
1578 bs_write( s, 8, i_region );
1579 bs_write( s, 4, p_sys->i_region_ver++ );
1581 /* Region attributes */
1582 bs_write( s, 1, i_bg < p_pal->i_entries ); /* Fill */
1583 bs_write( s, 3, 0 ); /* Reserved */
1584 bs_write( s, 16, p_sys->p_regions[i_region].i_width );
1585 bs_write( s, 16, p_sys->p_regions[i_region].i_height );
1586 bs_write( s, 3, i_depth ); /* Region level of compatibility */
1587 bs_write( s, 3, i_depth ); /* Region depth */
1588 bs_write( s, 2, 0 ); /* Reserved */
1589 bs_write( s, 8, 1 ); /* Clut id */
1590 bs_write( s, 8, i_bg ); /* region 8bit pixel code */
1591 bs_write( s, 4, i_bg ); /* region 4bit pixel code */
1592 bs_write( s, 2, i_bg ); /* region 2bit pixel code */
1593 bs_write( s, 2, 0 ); /* Reserved */
1595 /* In our implementation we only have 1 object per region */
1596 bs_write( s, 16, i_region );
1597 bs_write( s, 2, DVBSUB_OT_BASIC_BITMAP );
1598 bs_write( s, 2, 0 ); /* object provider flag */
1599 bs_write( s, 12, 0 ); /* object horizontal position */
1600 bs_write( s, 4, 0 ); /* Reserved */
1601 bs_write( s, 12, 0 ); /* object vertical position */
1605 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
1606 subpicture_region_t *p_region,
1609 static void encode_object( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
1611 encoder_sys_t *p_sys = p_enc->p_sys;
1612 subpicture_region_t *p_region;
1615 int i_length_pos, i_update_pos, i_pixel_data_pos;
1617 for( i_region = 0, p_region = p_subpic->p_region; p_region;
1618 p_region = p_region->p_next, i_region++ )
1620 bs_write( s, 8, 0x0f ); /* Sync byte */
1621 bs_write( s, 8, DVBSUB_ST_OBJECT_DATA ); /* Segment type */
1622 bs_write( s, 16, 1 ); /* Page id */
1624 i_length_pos = bs_pos( s );
1625 bs_write( s, 16, 0 ); /* Segment length */
1626 bs_write( s, 16, i_region ); /* Object id */
1627 bs_write( s, 4, p_sys->i_region_ver++ );
1628 bs_write( s, 2, 0 ); /* object coding method */
1630 bs_write( s, 1, 0 ); /* non modifying color flag */
1631 bs_write( s, 1, 0 ); /* Reserved */
1633 i_update_pos = bs_pos( s );
1634 bs_write( s, 16, 0 ); /* topfield data block length */
1635 bs_write( s, 16, 0 ); /* bottomfield data block length */
1638 i_pixel_data_pos = bs_pos( s );
1639 encode_pixel_data( p_enc, s, p_region, VLC_TRUE );
1640 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
1641 SetWBE( &s->p_start[i_update_pos/8], i_pixel_data_pos );
1644 i_pixel_data_pos = bs_pos( s );
1645 encode_pixel_data( p_enc, s, p_region, VLC_FALSE );
1646 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
1647 SetWBE( &s->p_start[i_update_pos/8+2], i_pixel_data_pos );
1649 /* Stuffing for word alignment */
1651 if( bs_pos( s ) % 16 ) bs_write( s, 8, 0 );
1653 /* Update segment length */
1654 SetWBE( &s->p_start[i_length_pos/8], (bs_pos(s) - i_length_pos)/8 -2 );
1658 static void encode_pixel_line_2bp( encoder_t *p_enc, bs_t *s,
1659 subpicture_region_t *p_region,
1661 static void encode_pixel_line_4bp( encoder_t *p_enc, bs_t *s,
1662 subpicture_region_t *p_region,
1664 static void encode_pixel_line_8bp( encoder_t *p_enc, bs_t *s,
1665 subpicture_region_t *p_region,
1667 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
1668 subpicture_region_t *p_region,
1671 unsigned int i_line;
1674 if( p_region->fmt.i_chroma != VLC_FOURCC('Y','U','V','P') ) return;
1676 /* Encode line by line */
1677 for( i_line = !b_top; i_line < p_region->fmt.i_visible_height;
1680 switch( p_region->fmt.p_palette->i_entries )
1683 bs_write( s, 8, 0x10 ); /* 2 bit/pixel code string */
1684 encode_pixel_line_2bp( p_enc, s, p_region, i_line );
1688 bs_write( s, 8, 0x11 ); /* 4 bit/pixel code string */
1689 encode_pixel_line_4bp( p_enc, s, p_region, i_line );
1693 bs_write( s, 8, 0x12 ); /* 8 bit/pixel code string */
1694 encode_pixel_line_8bp( p_enc, s, p_region, i_line );
1698 msg_Err( p_enc, "subpicture palette (%i) not handled",
1699 p_region->fmt.p_palette->i_entries );
1703 bs_write( s, 8, 0xf0 ); /* End of object line code */
1707 static void encode_pixel_line_2bp( encoder_t *p_enc, bs_t *s,
1708 subpicture_region_t *p_region,
1711 unsigned int i, i_length = 0;
1712 int i_pitch = p_region->picture.p->i_pitch;
1713 uint8_t *p_data = &p_region->picture.p->p_pixels[ i_pitch * i_line ];
1714 int i_last_pixel = p_data[0];
1716 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
1718 if( i != p_region->fmt.i_visible_width &&
1719 p_data[i] == i_last_pixel && i_length != 284 )
1725 if( i_length == 1 || i_length == 11 || i_length == 28 )
1727 /* 2bit/pixel code */
1728 if( i_last_pixel ) bs_write( s, 2, i_last_pixel );
1731 bs_write( s, 2, 0 );
1732 bs_write( s, 1, 0 );
1733 bs_write( s, 1, 1 ); /* pseudo color 0 */
1742 bs_write( s, 2, i_last_pixel );
1743 bs_write( s, 2, i_last_pixel );
1747 bs_write( s, 2, 0 );
1748 bs_write( s, 1, 0 );
1749 bs_write( s, 1, 0 );
1750 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
1753 else if( i_length > 2 )
1755 bs_write( s, 2, 0 );
1756 if( i_length <= 10 )
1758 bs_write( s, 1, 1 );
1759 bs_write( s, 3, i_length - 3 );
1760 bs_write( s, 2, i_last_pixel );
1764 bs_write( s, 1, 0 );
1765 bs_write( s, 1, 0 );
1767 if( i_length <= 27 )
1769 bs_write( s, 2, 2 );
1770 bs_write( s, 4, i_length - 12 );
1771 bs_write( s, 2, i_last_pixel );
1775 bs_write( s, 2, 3 );
1776 bs_write( s, 8, i_length - 29 );
1777 bs_write( s, 2, i_last_pixel );
1782 if( i == p_region->fmt.i_visible_width ) break;
1784 i_last_pixel = p_data[i];
1789 bs_write( s, 2, 0 );
1790 bs_write( s, 1, 0 );
1791 bs_write( s, 1, 0 );
1792 bs_write( s, 2, 0 );
1798 static void encode_pixel_line_4bp( encoder_t *p_enc, bs_t *s,
1799 subpicture_region_t *p_region,
1802 unsigned int i, i_length = 0;
1803 int i_pitch = p_region->picture.p->i_pitch;
1804 uint8_t *p_data = &p_region->picture.p->p_pixels[ i_pitch * i_line ];
1805 int i_last_pixel = p_data[0];
1807 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
1809 if( i != p_region->fmt.i_visible_width &&
1810 p_data[i] == i_last_pixel && i_length != 280 )
1816 if( i_length == 1 || (i_length == 3 && i_last_pixel) || i_length == 8 )
1818 /* 4bit/pixel code */
1819 if( i_last_pixel ) bs_write( s, 4, i_last_pixel );
1822 bs_write( s, 4, 0 );
1823 bs_write( s, 1, 1 );
1824 bs_write( s, 1, 1 );
1825 bs_write( s, 2, 0 ); /* pseudo color 0 */
1834 bs_write( s, 4, i_last_pixel );
1835 bs_write( s, 4, i_last_pixel );
1839 bs_write( s, 4, 0 );
1840 bs_write( s, 1, 1 );
1841 bs_write( s, 1, 1 );
1842 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
1845 else if( !i_last_pixel && i_length >= 3 && i_length <= 9 )
1847 bs_write( s, 4, 0 );
1848 bs_write( s, 1, 0 );
1849 bs_write( s, 3, i_length - 2 ); /* (i_length - 2) * color 0 */
1851 else if( i_length > 2 )
1853 bs_write( s, 4, 0 );
1854 bs_write( s, 1, 1 );
1858 bs_write( s, 1, 0 );
1859 bs_write( s, 2, i_length - 4 );
1860 bs_write( s, 4, i_last_pixel );
1864 bs_write( s, 1, 1 );
1866 if( i_length <= 24 )
1868 bs_write( s, 2, 2 );
1869 bs_write( s, 4, i_length - 9 );
1870 bs_write( s, 4, i_last_pixel );
1874 bs_write( s, 2, 3 );
1875 bs_write( s, 8, i_length - 25 );
1876 bs_write( s, 4, i_last_pixel );
1881 if( i == p_region->fmt.i_visible_width ) break;
1883 i_last_pixel = p_data[i];
1888 bs_write( s, 8, 0 );
1894 static void encode_pixel_line_8bp( encoder_t *p_enc, bs_t *s,
1895 subpicture_region_t *p_region,
1898 unsigned int i, i_length = 0;
1899 int i_pitch = p_region->picture.p->i_pitch;
1900 uint8_t *p_data = &p_region->picture.p->p_pixels[ i_pitch * i_line ];
1901 int i_last_pixel = p_data[0];
1903 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
1905 if( i != p_region->fmt.i_visible_width &&
1906 p_data[i] == i_last_pixel && i_length != 127 )
1912 if( i_length == 1 && i_last_pixel )
1914 /* 8bit/pixel code */
1915 bs_write( s, 8, i_last_pixel );
1917 else if( i_length == 2 && i_last_pixel )
1919 /* 8bit/pixel code */
1920 bs_write( s, 8, i_last_pixel );
1921 bs_write( s, 8, i_last_pixel );
1923 else if( i_length <= 127 )
1925 bs_write( s, 8, 0 );
1929 bs_write( s, 1, 0 );
1930 bs_write( s, 7, i_length ); /* pseudo color 0 */
1934 bs_write( s, 1, 1 );
1935 bs_write( s, 7, i_length );
1936 bs_write( s, 8, i_last_pixel );
1940 if( i == p_region->fmt.i_visible_width ) break;
1942 i_last_pixel = p_data[i];
1947 bs_write( s, 8, 0 );
1948 bs_write( s, 8, 0 );