1 /*****************************************************************************
2 * dvbsub.c : DVB subtitles decoder thread
3 *****************************************************************************
4 * Copyright (C) 2003 ANEVIA
5 * Copyright (C) 2003-2004 VideoLAN
8 * Authors: Damien LUCAS <damien.lucas@anevia.com>
9 * Laurent Aimar <fenrir@via.ecp.fr>
10 * Gildas Bazin <gbazin@videolan.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <vlc/decoder.h>
35 //#define DEBUG_DVBSUB 1
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
42 static subpicture_t *Decode( decoder_t *, block_t ** );
46 set_description( _("DVB subtitles decoder") );
47 set_capability( "decoder", 50 );
48 set_callbacks( Open, Close );
51 /****************************************************************************
53 ****************************************************************************
54 * Those structures refer closely to the ETSI 300 743 Object model
55 ****************************************************************************/
57 /* Storage of a RLE entry */
58 typedef struct dvbsub_rle_s
67 struct dvbsub_rle_s *p_next;
71 /* A subpicture image is a list of codes
72 * We need to store the length of each line since nothing specify in
73 * the standard that all lines should have the same length
74 * WARNING: We assume here that a spu is less than 576 lines high */
80 dvbsub_rle_t *p_codes;
84 /* The object definition gives the position of the object in a region */
85 typedef struct dvbsub_objectdef_s
97 /* An object is constituted of 2 images (for interleaving) */
98 typedef struct dvbsub_object_s
101 uint8_t i_version_number;
102 uint8_t i_coding_method;
103 vlc_bool_t b_non_modify_color;
104 dvbsub_image_t *topfield;
105 dvbsub_image_t *bottomfield;
106 struct dvbsub_object_s *p_next;
110 /* The object definition gives the position of the object in a region */
111 typedef struct dvbsub_regiondef_s
117 } dvbsub_regiondef_t;
119 /* The Region is an aera on the image
120 * with a list of the object definitions associated and a CLUT */
121 typedef struct dvbsub_region_s
124 uint8_t i_version_number;
130 uint8_t i_level_comp;
138 dvbsub_objectdef_t *p_object_defs;
140 struct dvbsub_region_s *p_next;
144 /* The page defines the list of regions */
150 uint8_t i_version_number;
152 uint8_t i_region_defs;
153 dvbsub_regiondef_t *p_region_defs;
157 /* The entry in the palette CLUT */
171 uint8_t i_version_number;
172 dvbsub_color_t c_2b[4];
173 dvbsub_color_t c_4b[16];
174 dvbsub_color_t c_8b[256];
182 /* Decoder internal data */
187 dvbsub_page_t *p_page;
188 dvbsub_region_t *p_regions;
189 dvbsub_object_t *p_objects;
191 dvbsub_clut_t *p_clut[256];
192 dvbsub_clut_t default_clut;
196 // List of different SEGMENT TYPES
197 // According to EN 300-743, table 2
198 #define DVBSUB_ST_PAGE_COMPOSITION 0x10
199 #define DVBSUB_ST_REGION_COMPOSITION 0x11
200 #define DVBSUB_ST_CLUT_DEFINITION 0x12
201 #define DVBSUB_ST_OBJECT_DATA 0x13
202 #define DVBSUB_ST_ENDOFDISPLAY 0x80
203 #define DVBSUB_ST_STUFFING 0xff
204 // List of different OBJECT TYPES
205 // According to EN 300-743, table 6
206 #define DVBSUB_OT_BASIC_BITMAP 0x00
207 #define DVBSUB_OT_BASIC_CHAR 0x01
208 #define DVBSUB_OT_COMPOSITE_STRING 0x02
210 // According to EN 300-743, table 9
211 #define DVBSUB_DT_2BP_CODE_STRING 0x10
212 #define DVBSUB_DT_4BP_CODE_STRING 0x11
213 #define DVBSUB_DT_8BP_CODE_STRING 0x12
214 #define DVBSUB_DT_24_TABLE_DATA 0x20
215 #define DVBSUB_DT_28_TABLE_DATA 0x21
216 #define DVBSUB_DT_48_TABLE_DATA 0x22
217 #define DVBSUB_DT_END_LINE 0xf0
218 // List of different Page Composition Segment state
219 // According to EN 300-743, 7.2.1 table 3
220 #define DVBSUB_PCS_STATE_ACQUISITION 0x01
221 #define DVBSUB_PCS_STATE_CHANGE 0x10
223 /*****************************************************************************
225 *****************************************************************************/
226 static void decode_segment( decoder_t *, bs_t * );
227 static void decode_page_composition( decoder_t *, bs_t * );
228 static void decode_region_composition( decoder_t *, bs_t * );
229 static void decode_object( decoder_t *, bs_t * );
230 static void decode_clut( decoder_t *, bs_t * );
232 static void free_objects( decoder_t * );
233 static void free_all( decoder_t * );
235 static subpicture_t *render( decoder_t * );
237 static void default_clut_init( decoder_t * );
239 /*****************************************************************************
240 * Open: probe the decoder and return score
241 *****************************************************************************
242 * Tries to launch a decoder and return score so that the interface is able
244 *****************************************************************************/
245 static int Open( vlc_object_t *p_this )
247 decoder_t *p_dec = (decoder_t *) p_this;
248 decoder_sys_t *p_sys;
251 if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','v','b','s') )
256 p_dec->pf_decode_sub = Decode;
257 p_sys = p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
260 p_sys->i_id = p_dec->fmt_in.subs.dvb.i_id & 0xFFFF;
261 p_sys->i_ancillary_id = p_dec->fmt_in.subs.dvb.i_id >> 16;
262 p_sys->p_page = NULL;
263 p_sys->p_regions = NULL;
264 p_sys->p_objects = NULL;
265 for( i = 0; i < 256; i++ ) p_sys->p_clut[i] = NULL;
267 es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'd','v','b','s' ) );
269 default_clut_init( p_dec );
274 /*****************************************************************************
276 *****************************************************************************/
277 static void Close( vlc_object_t *p_this )
279 decoder_t *p_dec = (decoder_t*) p_this;
280 decoder_sys_t *p_sys = p_dec->p_sys;
286 /*****************************************************************************
288 *****************************************************************************/
289 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
291 decoder_sys_t *p_sys = p_dec->p_sys;
293 subpicture_t *p_spu = NULL;
295 if( pp_block == NULL || *pp_block == NULL ) return NULL;
299 p_sys->i_pts = p_block->i_pts;
300 if( p_sys->i_pts <= 0 )
302 msg_Warn( p_dec, "non dated subtitle" );
303 block_Release( p_block );
307 bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
309 if( bs_read( &p_sys->bs, 8 ) != 0x20 ) /* Data identifier */
311 msg_Dbg( p_dec, "invalid data identifier" );
312 block_Release( p_block );
316 if( bs_read( &p_sys->bs, 8 ) != 0x20 && 0 ) /* Subtitle stream id */
318 msg_Dbg( p_dec, "invalid subtitle stream id" );
319 block_Release( p_block );
323 while( bs_show( &p_sys->bs, 8 ) == 0x0f ) /* Sync byte */
325 decode_segment( p_dec, &p_sys->bs );
328 if( bs_read( &p_sys->bs, 8 ) != 0xff ) /* End marker */
330 msg_Warn( p_dec, "end marker not found (corrupted subtitle ?)" );
331 block_Release( p_block );
335 /* Check if the page is to be displayed */
336 if( p_sys->p_page ) p_spu = render( p_dec );
338 block_Release( p_block );
343 /* following functions are local */
345 /*****************************************************************************
346 * default_clut_init: default clut as defined in EN 300-743 section 10
347 *****************************************************************************/
348 static void default_clut_init( decoder_t *p_dec )
350 decoder_sys_t *p_sys = p_dec->p_sys;
353 #define RGB_TO_Y(r, g, b) ((int16_t) 77 * r + 150 * g + 29 * b) / 256;
354 #define RGB_TO_U(r, g, b) ((int16_t) -44 * r - 87 * g + 131 * b) / 256;
355 #define RGB_TO_V(r, g, b) ((int16_t) 131 * r - 110 * g - 21 * b) / 256;
358 for( i = 0; i < 4; i++ )
360 uint8_t R = 0, G = 0, B = 0, T = 0;
362 if( !(i & 0x2) && !(i & 0x1) ) T = 0xFF;
363 else if( !(i & 0x2) && (i & 0x1) ) R = G = B = 0xFF;
364 else if( (i & 0x2) && !(i & 0x1) ) R = G = B = 0;
365 else R = G = B = 0x7F;
367 p_sys->default_clut.c_2b[i].Y = RGB_TO_Y(R,G,B);
368 p_sys->default_clut.c_2b[i].Cr = RGB_TO_U(R,G,B);
369 p_sys->default_clut.c_2b[i].Cb = RGB_TO_V(R,G,B);
370 p_sys->default_clut.c_2b[i].T = T;
373 /* 16 entries CLUT */
374 for( i = 0; i < 16; i++ )
376 uint8_t R = 0, G = 0, B = 0, T = 0;
380 if( !(i & 0x4) && !(i & 0x2) && !(i & 0x1) )
386 R = (i & 0x1) ? 0xFF : 0;
387 G = (i & 0x2) ? 0xFF : 0;
388 B = (i & 0x4) ? 0xFF : 0;
393 R = (i & 0x1) ? 0x7F : 0;
394 G = (i & 0x2) ? 0x7F : 0;
395 B = (i & 0x4) ? 0x7F : 0;
398 p_sys->default_clut.c_4b[i].Y = RGB_TO_Y(R,G,B);
399 p_sys->default_clut.c_4b[i].Cr = RGB_TO_U(R,G,B);
400 p_sys->default_clut.c_4b[i].Cb = RGB_TO_V(R,G,B);
401 p_sys->default_clut.c_4b[i].T = T;
404 /* 256 entries CLUT (TODO) */
405 memset( p_sys->default_clut.c_8b, 0xFF, 256 * sizeof(dvbsub_color_t) );
408 static void decode_segment( decoder_t *p_dec, bs_t *s )
410 decoder_sys_t *p_sys = p_dec->p_sys;
415 /* sync_byte (already checked) */
419 i_type = bs_read( s, 8 );
422 i_page_id = bs_read( s, 16 );
425 i_size = bs_show( s, 16 );
427 if( i_page_id != p_sys->i_id && i_page_id != p_sys->i_ancillary_id )
430 msg_Dbg( p_dec, "subtitle skipped (page id: %i)", i_page_id );
432 bs_skip( s, 8 * ( 2 + i_size ) );
437 if( i_page_id == p_sys->i_id )
438 msg_Dbg( p_dec, "segment (id: %i)", i_page_id );
440 msg_Dbg( p_dec, "ancillary segment (id: %i)", i_page_id );
445 case DVBSUB_ST_PAGE_COMPOSITION:
447 msg_Dbg( p_dec, "decode_page_composition" );
449 decode_page_composition( p_dec, s );
452 case DVBSUB_ST_REGION_COMPOSITION:
454 msg_Dbg( p_dec, "decode_region_composition" );
456 decode_region_composition( p_dec, s );
459 case DVBSUB_ST_CLUT_DEFINITION:
461 msg_Dbg( p_dec, "decode_clut" );
463 decode_clut( p_dec, s );
466 case DVBSUB_ST_OBJECT_DATA:
468 msg_Dbg( p_dec, "decode_object" );
470 decode_object( p_dec, s );
473 case DVBSUB_ST_ENDOFDISPLAY:
475 msg_Dbg( p_dec, "end of display" );
477 bs_skip( s, 8 * ( 2 + i_size ) );
480 case DVBSUB_ST_STUFFING:
482 msg_Dbg( p_dec, "skip stuffing" );
484 bs_skip( s, 8 * ( 2 + i_size ) );
488 msg_Warn( p_dec, "unsupported segment type: (%04x)", i_type );
489 bs_skip( s, 8 * ( 2 + i_size ) );
494 static void decode_clut( decoder_t *p_dec, bs_t *s )
496 decoder_sys_t *p_sys = p_dec->p_sys;
497 uint16_t i_segment_length;
498 uint16_t i_processed_length;
499 dvbsub_clut_t *p_clut;
501 uint8_t i_version_number;
503 i_segment_length = bs_read( s, 16 );
504 i_clut_id = bs_read( s, 8 );
505 i_version_number = bs_read( s, 4 );
507 /* Check that this id doesn't not already exist with the same version
508 * number and allocate memory if necessary */
509 if( p_sys->p_clut[i_clut_id] != NULL &&
510 p_sys->p_clut[i_clut_id]->i_version_number == i_version_number )
513 bs_skip( s, 8 * i_segment_length - 12 );
517 if( !p_sys->p_clut[i_clut_id] )
519 p_sys->p_clut[i_clut_id] = malloc( sizeof(dvbsub_clut_t) );
522 p_clut = p_sys->p_clut[i_clut_id];
524 /* We don't have this version of the CLUT: Parse it */
525 p_clut->i_version_number = i_version_number;
526 bs_skip( s, 4 ); /* Reserved bits */
527 i_processed_length = 2;
528 while( i_processed_length < i_segment_length )
530 uint8_t y, cb, cr, t;
534 i_id = bs_read( s, 8 );
535 i_type = bs_read( s, 3 );
539 if( bs_read( s, 1 ) )
542 cr = bs_read( s, 8 );
543 cb = bs_read( s, 8 );
545 i_processed_length += 6;
550 cr = bs_read( s, 4 );
551 cb = bs_read( s, 4 );
553 i_processed_length += 4;
556 /* According to EN 300-743 section 7.2.3 note 1, type should
557 * not have more than 1 bit set to one, but some strams don't
558 * respect this note. */
562 p_clut->c_2b[i_id].Y = y;
563 p_clut->c_2b[i_id].Cr = cr;
564 p_clut->c_2b[i_id].Cb = cb;
565 p_clut->c_2b[i_id].T = t;
569 p_clut->c_4b[i_id].Y = y;
570 p_clut->c_4b[i_id].Cr = cr;
571 p_clut->c_4b[i_id].Cb = cb;
572 p_clut->c_4b[i_id].T = t;
576 p_clut->c_8b[i_id].Y = y;
577 p_clut->c_8b[i_id].Cr = cr;
578 p_clut->c_8b[i_id].Cb = cb;
579 p_clut->c_8b[i_id].T = t;
584 static void decode_page_composition( decoder_t *p_dec, bs_t *s )
586 decoder_sys_t *p_sys = p_dec->p_sys;
587 unsigned int i_version_number;
588 unsigned int i_state;
589 unsigned int i_segment_length;
593 /* A page is composed by one or more region */
595 i_segment_length = bs_read( s, 16 );
596 i_timeout = bs_read( s, 8 );
597 i_version_number = bs_read( s, 4 );
598 i_state = bs_read( s, 2 );
599 bs_skip( s, 2 ); /* Reserved */
601 if( i_state == DVBSUB_PCS_STATE_CHANGE )
603 /* End of an epoch, reset decoder buffer */
605 msg_Dbg( p_dec, "page composition mode change" );
609 else if( !p_sys->p_page && i_state != DVBSUB_PCS_STATE_ACQUISITION )
611 /* Not a full PCS, we need to wait for one */
615 if( i_state == DVBSUB_PCS_STATE_ACQUISITION )
617 /* Make sure we clean up regularly our objects list.
618 * Is it the best place to do this ? */
619 free_objects( p_dec );
622 /* Check version number */
624 p_sys->p_page->i_version_number == i_version_number )
626 bs_skip( s, 8 * (i_segment_length - 2) );
629 else if( p_sys->p_page )
631 if( p_sys->p_page->i_region_defs )
632 free( p_sys->p_page->p_region_defs );
633 p_sys->p_page->i_region_defs = 0;
638 /* Allocate a new page */
639 p_sys->p_page = malloc( sizeof(dvbsub_page_t) );
642 p_sys->p_page->i_version_number = i_version_number;
643 p_sys->p_page->i_timeout = i_timeout;
645 /* Number of regions */
646 p_sys->p_page->i_region_defs = (i_segment_length - 2) / 6;
648 if( p_sys->p_page->i_region_defs == 0 ) return;
650 p_sys->p_page->p_region_defs =
651 malloc( p_sys->p_page->i_region_defs * sizeof(dvbsub_region_t) );
652 for( i = 0; i < p_sys->p_page->i_region_defs; i++ )
654 p_sys->p_page->p_region_defs[i].i_id = bs_read( s, 8 );
655 bs_skip( s, 8 ); /* Reserved */
656 p_sys->p_page->p_region_defs[i].i_x = bs_read( s, 16 );
657 p_sys->p_page->p_region_defs[i].i_y = bs_read( s, 16 );
660 msg_Dbg( p_dec, "page_composition, region %i (%i,%i)",
661 i, p_sys->p_page->p_region_defs[i].i_x,
662 p_sys->p_page->p_region_defs[i].i_y );
667 static void decode_region_composition( decoder_t *p_dec, bs_t *s )
669 decoder_sys_t *p_sys = p_dec->p_sys;
670 dvbsub_region_t *p_region, **pp_region = &p_sys->p_regions;
671 int i_segment_length;
672 int i_processed_length;
674 int i_version_number;
676 i_segment_length = bs_read( s, 16 );
677 i_region_id = bs_read( s, 8 );
678 i_version_number = bs_read( s, 4 );
680 /* Check if we already have this region */
681 for( p_region = p_sys->p_regions; p_region != NULL;
682 p_region = p_region->p_next )
684 pp_region = &p_region->p_next;
685 if( p_region->i_id == i_region_id ) break;
688 /* Check version number */
690 p_region->i_version_number == i_version_number )
692 bs_skip( s, 8 * (i_segment_length - 1) - 4 );
697 if( p_region->i_object_defs )
698 free( p_region->p_object_defs );
704 msg_Dbg( p_dec, "new region: %i", i_region_id );
706 p_region = *pp_region = malloc( sizeof(dvbsub_region_t) );
707 p_region->p_next = NULL;
710 /* Region attributes */
711 p_region->i_id = i_region_id;
712 p_region->i_version_number = i_version_number;
713 p_region->b_fill = bs_read( s, 1 );
714 bs_skip( s, 3 ); /* Reserved */
715 p_region->i_width = bs_read( s, 16 );
716 p_region->i_height = bs_read( s, 16 );
717 p_region->i_level_comp = bs_read( s, 3 );
718 p_region->i_depth = bs_read( s, 3 );
719 bs_skip( s, 2 ); /* Reserved */
720 p_region->i_clut = bs_read( s, 8 );
721 p_region->i_8bp_code = bs_read( s, 8 );
722 p_region->i_4bp_code = bs_read( s, 4 );
723 p_region->i_2bp_code = bs_read( s, 2 );
724 bs_skip( s, 2 ); /* Reserved */
725 p_region->p_object_defs = NULL;
726 p_region->i_object_defs = 0;
728 /* List of objects in the region */
729 i_processed_length = 10;
730 while( i_processed_length < i_segment_length )
732 dvbsub_objectdef_t *p_obj;
734 /* We create a new object */
735 p_region->i_object_defs++;
736 p_region->p_object_defs =
737 realloc( p_region->p_object_defs,
738 sizeof(dvbsub_objectdef_t) * p_region->i_object_defs );
740 /* We parse object properties */
741 p_obj = &p_region->p_object_defs[p_region->i_object_defs - 1];
742 p_obj->i_id = bs_read( s, 16 );
743 p_obj->i_type = bs_read( s, 2 );
744 p_obj->i_provider = bs_read( s, 2 );
745 p_obj->i_x = bs_read( s, 12 );
746 bs_skip( s, 4 ); /* Reserved */
747 p_obj->i_y = bs_read( s, 12 );
749 i_processed_length += 6;
751 if( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ||
752 p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING )
754 p_obj->i_fg_pc = bs_read( s, 8 );
755 p_obj->i_bg_pc = bs_read( s, 8 );
756 i_processed_length += 2;
761 static dvbsub_image_t *dvbsub_parse_pdata( decoder_t *, bs_t *, uint16_t );
762 static uint16_t dvbsub_pdata2bpp( bs_t *, uint16_t *, dvbsub_image_t * );
763 static uint16_t dvbsub_pdata4bpp( bs_t *, uint16_t *, dvbsub_image_t * );
764 static uint16_t dvbsub_pdata8bpp( bs_t *, uint16_t *, dvbsub_image_t * );
766 static void decode_object( decoder_t *p_dec, bs_t *s )
768 decoder_sys_t *p_sys = p_dec->p_sys;
769 dvbsub_object_t *p_obj, **pp_obj = &p_sys->p_objects;
770 int i_segment_length;
771 int i_version_number;
775 i_segment_length = bs_read( s, 16 );
776 i_obj_id = bs_read( s, 16 );
777 i_version_number = bs_read( s, 4 );
778 i_coding_method = bs_read( s, 2 );
780 if( i_coding_method )
782 /* TODO: DVB subtitling as characters */
783 msg_Dbg( p_dec, "DVB subtitling as characters is not handled!" );
784 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
788 /* Check if we already have this region */
789 for( p_obj = p_sys->p_objects; p_obj != NULL; p_obj = p_obj->p_next )
791 pp_obj = &p_obj->p_next;
792 if( p_obj->i_id == i_obj_id ) break;
795 /* Check version number */
796 if( p_obj && p_obj->i_version_number == i_version_number )
798 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
803 /* Clean structure */
809 msg_Dbg( p_dec, "new object: %i", i_obj_id );
811 p_obj = *pp_obj = malloc( sizeof(dvbsub_object_t) );
812 p_obj->p_next = NULL;
815 p_obj->i_id = i_obj_id;
816 p_obj->i_version_number = i_version_number;
817 p_obj->i_coding_method = i_coding_method;
818 p_obj->b_non_modify_color = bs_read( s, 1 );
819 bs_skip( s, 1 ); /* Reserved */
821 if( p_obj->i_coding_method == 0x00 )
823 uint16_t i_topfield_length;
824 uint16_t i_bottomfield_length;
826 i_topfield_length = bs_read( s, 16 );
827 i_bottomfield_length = bs_read( s, 16 );
830 dvbsub_parse_pdata( p_dec, s, i_topfield_length );
832 dvbsub_parse_pdata( p_dec, s, i_bottomfield_length );
836 /* TODO: DVB subtitling as characters */
840 static dvbsub_image_t* dvbsub_parse_pdata( decoder_t *p_dec, bs_t *s,
843 dvbsub_image_t* p_image;
844 uint16_t i_processed_length = 0;
845 uint16_t i_lines = 0;
846 uint16_t i_cols_last = 0;
848 p_image = malloc( sizeof(dvbsub_image_t) );
849 p_image->p_last = NULL;
851 memset( p_image->i_cols, 0, 576 * sizeof(uint16_t) );
853 /* Let's parse it a first time to determine the size of the buffer */
854 while( i_processed_length < length)
856 i_processed_length++;
858 switch( bs_read( s, 8 ) )
861 i_processed_length +=
862 dvbsub_pdata2bpp( s, &p_image->i_cols[i_lines], p_image );
865 i_processed_length +=
866 dvbsub_pdata4bpp( s, &p_image->i_cols[i_lines], p_image );
869 i_processed_length +=
870 dvbsub_pdata8bpp( s, &p_image->i_cols[i_lines], p_image );
875 /* We don't use map tables */
878 i_lines++; /* End of line code */
883 p_image->i_rows = i_lines;
884 p_image->i_cols[i_lines] = i_cols_last;
886 /* Check word-aligned bits */
887 if( bs_show( s, 8 ) == 0x00 )
895 static void add_rle_code( dvbsub_image_t *p, uint16_t num, uint8_t color,
898 if( p->p_last != NULL )
900 p->p_last->p_next = malloc( sizeof(dvbsub_rle_t) );
901 p->p_last = p->p_last->p_next;
905 p->p_codes = malloc( sizeof(dvbsub_rle_t) );
906 p->p_last = p->p_codes;
908 p->p_last->i_num = num;
910 p->p_last->i_color_code = color;
911 p->p_last->i_bpp = i_bpp;
912 p->p_last->p_next = NULL;
915 static uint16_t dvbsub_pdata2bpp( bs_t *s, uint16_t* p,
916 dvbsub_image_t* p_image )
918 uint16_t i_processed = 0;
919 vlc_bool_t b_stop = 0;
920 uint16_t i_count = 0;
926 if( (i_color = bs_read( s, 2 )) != 0x00 )
931 add_rle_code( p_image, 1, i_color, 2 );
936 if( bs_read( s, 1 ) == 0x00 ) // Switch1
938 i_count = 3 + bs_read( s, 3 );
940 i_color = bs_read( s, 2 );
941 add_rle_code( p_image, i_count, i_color, 2 );
947 if( bs_read( s, 1 ) == 0x00 ) //Switch2
950 switch( bs_read( s, 2 ) ) //Switch3
956 add_rle_code( p_image, 2, 0, 2 );
959 i_count = 12 + bs_read( s, 4 );
960 i_color = bs_read( s, 2 );
963 add_rle_code( p_image, i_count, i_color, 2 );
966 i_count = 29 + bs_read( s, 8 );
967 i_color = bs_read( s, 2 );
970 add_rle_code( p_image, i_count, i_color, 2 );
982 return ( i_processed + 7 ) / 8 ;
985 static uint16_t dvbsub_pdata4bpp( bs_t *s, uint16_t* p,
986 dvbsub_image_t* p_image )
988 uint16_t i_processed = 0;
989 vlc_bool_t b_stop = 0;
990 uint16_t i_count = 0;
995 if( (i_color = bs_read( s, 4 )) != 0x00 )
1001 add_rle_code( p_image, 1, i_color, 4 );
1005 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1007 if( bs_show( s, 3 ) != 0x00 )
1009 i_count = 2 + bs_read( s, 3 );
1011 add_rle_code( p_image, i_count, 0x00, 4 );
1022 if( bs_read( s, 1 ) == 0x00) //Switch2
1024 i_count = 4 + bs_read( s, 2 );
1025 i_color = bs_read( s, 4 );
1028 add_rle_code( p_image, i_count, i_color, 4 );
1032 switch ( bs_read( s, 2 ) ) //Switch3
1037 add_rle_code( p_image, 1, 0x00, 4 );
1042 add_rle_code( p_image, 2, 0x00, 4 );
1045 i_count = 9 + bs_read( s, 4 );
1046 i_color = bs_read( s, 4 );
1049 add_rle_code( p_image, i_count, i_color, 4 );
1052 i_count= 25 + bs_read( s, 8 );
1053 i_color = bs_read( s, 4 );
1056 add_rle_code( p_image, i_count, i_color, 4 );
1066 return ( i_processed + 7 ) / 8 ;
1069 static uint16_t dvbsub_pdata8bpp( bs_t *s, uint16_t* p,
1070 dvbsub_image_t* p_image )
1072 uint16_t i_processed = 0;
1073 vlc_bool_t b_stop = 0;
1074 uint16_t i_count = 0;
1075 uint8_t i_color = 0;
1080 if( (i_color = bs_read( s, 8 )) != 0x00 )
1085 add_rle_code( p_image, 1, i_color, 8 );
1090 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1092 if( bs_show( s, 7 ) != 0x00 )
1094 i_count = bs_read( s, 7 );
1096 add_rle_code( p_image, i_count, 0x00, 8 );
1107 i_count = bs_read( s, 7 );
1109 i_color = bs_read( s, 8 );
1110 add_rle_code( p_image, i_count, i_color, 8 );
1118 return ( i_processed + 7 ) / 8 ;
1121 static void free_image( dvbsub_image_t *p_i )
1124 dvbsub_rle_t *p2 = NULL;
1126 for( p1 = p_i->p_codes; p1 != NULL; p1 = p2 )
1136 static void free_objects( decoder_t *p_dec )
1138 decoder_sys_t *p_sys = p_dec->p_sys;
1139 dvbsub_object_t *p_obj, *p_obj_next;
1141 for( p_obj = p_sys->p_objects; p_obj != NULL; p_obj = p_obj_next )
1143 p_obj_next = p_obj->p_next;
1144 free_image( p_obj->topfield );
1145 free_image( p_obj->bottomfield );
1148 p_sys->p_objects = NULL;
1151 static void free_all( decoder_t *p_dec )
1153 decoder_sys_t *p_sys = p_dec->p_sys;
1154 dvbsub_region_t *p_reg, *p_reg_next;
1157 for( i = 0; i < 256; i++ )
1159 if( p_sys->p_clut[i] ) free( p_sys->p_clut[i] );
1160 p_sys->p_clut[i] = NULL;
1165 if( p_sys->p_page->i_region_defs )
1166 free( p_sys->p_page->p_region_defs );
1167 free( p_sys->p_page );
1168 p_sys->p_page = NULL;
1171 for( p_reg = p_sys->p_regions; p_reg != NULL; p_reg = p_reg_next )
1173 p_reg_next = p_reg->p_next;
1174 if( p_reg->i_object_defs ) free( p_reg->p_object_defs );
1177 p_sys->p_regions = NULL;
1179 free_objects( p_dec );
1182 static subpicture_t *render( decoder_t *p_dec )
1184 decoder_sys_t *p_sys = p_dec->p_sys;
1185 dvbsub_clut_t *p_clut;
1187 subpicture_t *p_spu;
1188 subpicture_region_t **pp_spu_region;
1189 int i, j = 0, i_timeout = 0;
1191 /* Allocate the subpicture internal data. */
1192 p_spu = p_dec->pf_spu_buffer_new( p_dec );
1193 if( !p_spu ) return NULL;
1195 pp_spu_region = &p_spu->p_region;
1197 /* Loop on region definitions */
1200 msg_Dbg( p_dec, "rendering %i regions", p_sys->p_page->i_region_defs );
1203 for( i = 0; p_sys->p_page && i < p_sys->p_page->i_region_defs; i++ )
1205 dvbsub_region_t *p_region;
1206 dvbsub_regiondef_t *p_regiondef;
1207 subpicture_region_t *p_spu_region;
1208 uint8_t *p_y, *p_u, *p_v, *p_a;
1212 i_timeout = p_sys->p_page->i_timeout;
1214 p_regiondef = &p_sys->p_page->p_region_defs[i];
1217 msg_Dbg( p_dec, "rendering region %i (%i,%i)", i,
1218 p_regiondef->i_x, p_regiondef->i_y );
1221 /* Find associated region */
1222 for( p_region = p_sys->p_regions; p_region != NULL;
1223 p_region = p_region->p_next )
1225 if( p_regiondef->i_id == p_region->i_id ) break;
1230 msg_Err( p_dec, "no region founddddd!!!" );
1234 /* Create new SPU region */
1235 memset( &fmt, 0, sizeof(video_format_t) );
1236 fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
1237 fmt.i_aspect = VOUT_ASPECT_FACTOR;
1238 fmt.i_width = fmt.i_visible_width = p_region->i_width;
1239 fmt.i_height = fmt.i_visible_height = p_region->i_height;
1240 fmt.i_x_offset = fmt.i_y_offset = 0;
1241 p_spu_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
1244 msg_Err( p_dec, "cannot allocate SPU region" );
1247 p_spu_region->i_x = p_regiondef->i_x;
1248 p_spu_region->i_y = p_regiondef->i_y;
1249 *pp_spu_region = p_spu_region;
1250 pp_spu_region = &p_spu_region->p_next;
1252 p_y = p_spu_region->picture.Y_PIXELS;
1253 p_u = p_spu_region->picture.U_PIXELS;
1254 p_v = p_spu_region->picture.V_PIXELS;
1255 p_a = p_spu_region->picture.A_PIXELS;
1256 i_pitch = p_spu_region->picture.Y_PITCH;
1257 memset( p_a, 0, i_pitch * p_region->i_height );
1259 /* Loop on object definitions */
1260 for( j = 0; j < p_region->i_object_defs; j++ )
1262 dvbsub_object_t *p_object;
1263 dvbsub_objectdef_t *p_objectdef;
1264 uint16_t k, l, x, y;
1266 p_objectdef = &p_region->p_object_defs[j];
1269 msg_Dbg( p_dec, "rendering object %i (%i,%i)", p_objectdef->i_id,
1270 p_objectdef->i_x, p_objectdef->i_y );
1273 /* Look for the right object */
1274 for( p_object = p_sys->p_objects; p_object != NULL;
1275 p_object = p_object->p_next )
1277 if( p_objectdef->i_id == p_object->i_id ) break;
1282 msg_Err( p_dec, "no object founddddd!!!" );
1286 /* Draw SPU region */
1287 p_clut = p_sys->p_clut[p_region->i_clut];
1288 if( !p_clut ) p_clut = &p_sys->default_clut;
1290 for( k = 0, l = 0, p_c = p_object->topfield->p_codes;
1291 p_c->p_next; p_c = p_c->p_next )
1293 /* Compute the color data according to the appropriate CLUT */
1294 dvbsub_color_t *p_color = (p_c->i_bpp == 2) ? p_clut->c_2b :
1295 (p_c->i_bpp == 4) ? p_clut->c_4b : p_clut->c_8b;
1297 x = l + p_objectdef->i_x;
1298 y = 2 * k + p_objectdef->i_y;
1299 memset( p_y + y * i_pitch + x, p_color[p_c->i_color_code].Y,
1301 memset( p_u + y * i_pitch + x, p_color[p_c->i_color_code].Cr,
1303 memset( p_v + y * i_pitch + x, p_color[p_c->i_color_code].Cb,
1305 memset( p_a + y * i_pitch + x,
1306 255 - p_color[p_c->i_color_code].T, p_c->i_num );
1309 if( l >= p_object->topfield->i_cols[k] ) { k++; l = 0; }
1310 if( k >= p_object->topfield->i_rows) break;
1314 for( k = 0, l = 0, p_c = p_object->bottomfield->p_codes;
1315 p_c->p_next; p_c = p_c->p_next )
1317 /* Compute the color data according to the appropriate CLUT */
1318 dvbsub_color_t *p_color = (p_c->i_bpp == 2) ? p_clut->c_2b :
1319 (p_c->i_bpp == 4) ? p_clut->c_4b : p_clut->c_8b;
1321 x = l + p_objectdef->i_x;
1322 y = 2 * k + 1 + p_objectdef->i_y;
1323 memset( p_y + y * i_pitch + x, p_color[p_c->i_color_code].Y,
1325 memset( p_u + y * i_pitch + x, p_color[p_c->i_color_code].Cr,
1327 memset( p_v + y * i_pitch + x, p_color[p_c->i_color_code].Cb,
1329 memset( p_a + y * i_pitch + x,
1330 255 - p_color[p_c->i_color_code].T, p_c->i_num );
1333 if( l >= p_object->bottomfield->i_cols[k] ) { k++; l = 0; }
1334 if( k >= p_object->bottomfield->i_rows) break;
1340 /* Set the pf_render callback */
1341 p_spu->i_start = p_sys->i_pts;
1342 p_spu->i_stop = p_spu->i_start + i_timeout * 1000000;
1343 p_spu->b_ephemer = VLC_TRUE;