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 *p_this );
41 static void Close( vlc_object_t *p_this );
44 set_description( _("DVB subtitles decoder") );
45 set_capability( "decoder", 50 );
46 set_callbacks( Open, Close );
49 /****************************************************************************
51 ****************************************************************************
52 * Those structures refer closely to the ETSI 300 743 Object model
53 ****************************************************************************/
55 /* Storage of a RLE entry */
56 typedef struct dvbsub_rle_s
65 struct dvbsub_rle_s *p_next;
69 /* A subpicture image is a list of codes
70 * We need to store the length of each line since nothing specify in
71 * the standard that all lines should have the same length
72 * WARNING: We assume here that a spu is less than 576 lines high */
78 dvbsub_rle_t *p_codes;
82 /* The object definition gives the position of the object in a region */
83 typedef struct dvbsub_objectdef_s
95 /* An object is constituted of 2 images (for interleaving) */
96 typedef struct dvbsub_object_s
99 uint8_t i_version_number;
100 uint8_t i_coding_method;
101 vlc_bool_t b_non_modify_color;
102 dvbsub_image_t *topfield;
103 dvbsub_image_t *bottomfield;
104 struct dvbsub_object_s *p_next;
108 /* The object definition gives the position of the object in a region */
109 typedef struct dvbsub_regiondef_s
115 } dvbsub_regiondef_t;
117 /* The Region is an aera on the image
118 * with a list of the object definitions associated and a CLUT */
119 typedef struct dvbsub_region_s
122 uint8_t i_version_number;
128 uint8_t i_level_comp;
136 dvbsub_objectdef_t *p_object_defs;
138 struct dvbsub_region_s *p_next;
142 /* The page defines the list of regions */
148 uint8_t i_version_number;
150 uint8_t i_region_defs;
151 dvbsub_regiondef_t *p_region_defs;
155 /* The entry in the palette CLUT */
169 uint8_t i_version_number;
170 dvbsub_color_t c_2b[4];
171 dvbsub_color_t c_4b[16];
172 dvbsub_color_t c_8b[256];
176 typedef struct dvbsub_render_s
180 dvbsub_image_t *p_rle_top;
181 dvbsub_image_t *p_rle_bot;
183 struct dvbsub_render_s *p_next;
187 struct subpicture_sys_t
189 dvbsub_render_t *p_objects; /* Linked list of objects to render */
194 vout_thread_t *p_vout;
198 /* Decoder internal data */
204 dvbsub_page_t *p_page;
205 dvbsub_region_t *p_regions;
206 dvbsub_object_t *p_objects;
208 dvbsub_clut_t *p_clut[256];
209 dvbsub_clut_t default_clut;
212 int i_subpic_channel;
216 // List of different SEGMENT TYPES
217 // According to EN 300-743, table 2
218 #define DVBSUB_ST_PAGE_COMPOSITION 0x10
219 #define DVBSUB_ST_REGION_COMPOSITION 0x11
220 #define DVBSUB_ST_CLUT_DEFINITION 0x12
221 #define DVBSUB_ST_OBJECT_DATA 0x13
222 #define DVBSUB_ST_ENDOFDISPLAY 0x80
223 #define DVBSUB_ST_STUFFING 0xff
224 // List of different OBJECT TYPES
225 // According to EN 300-743, table 6
226 #define DVBSUB_OT_BASIC_BITMAP 0x00
227 #define DVBSUB_OT_BASIC_CHAR 0x01
228 #define DVBSUB_OT_COMPOSITE_STRING 0x02
230 // According to EN 300-743, table 9
231 #define DVBSUB_DT_2BP_CODE_STRING 0x10
232 #define DVBSUB_DT_4BP_CODE_STRING 0x11
233 #define DVBSUB_DT_8BP_CODE_STRING 0x12
234 #define DVBSUB_DT_24_TABLE_DATA 0x20
235 #define DVBSUB_DT_28_TABLE_DATA 0x21
236 #define DVBSUB_DT_48_TABLE_DATA 0x22
237 #define DVBSUB_DT_END_LINE 0xf0
238 // List of different Page Composition Segment state
239 // According to EN 300-743, 7.2.1 table 3
240 #define DVBSUB_PCS_STATE_ACQUISITION 0x01
241 #define DVBSUB_PCS_STATE_CHANGE 0x10
243 /*****************************************************************************
245 *****************************************************************************/
246 static void Decode( decoder_t *, block_t ** );
248 static void decode_segment( decoder_t *, bs_t * );
249 static void decode_page_composition( decoder_t *, bs_t * );
250 static void decode_region_composition( decoder_t *, bs_t * );
251 static void decode_object( decoder_t *, bs_t * );
252 static void decode_clut( decoder_t *, bs_t * );
254 static void free_objects( decoder_t * );
255 static void free_all( decoder_t * );
257 static void render( decoder_t *, vout_thread_t * );
258 static void default_clut_init( decoder_t * );
260 /*****************************************************************************
261 * Open: probe the decoder and return score
262 *****************************************************************************
263 * Tries to launch a decoder and return score so that the interface is able
265 *****************************************************************************/
266 static int Open( vlc_object_t *p_this )
268 decoder_t *p_dec = (decoder_t *) p_this;
269 decoder_sys_t *p_sys;
272 if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','v','b','s') )
277 p_dec->pf_decode_sub = Decode;
278 p_sys = p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
281 p_sys->i_id = p_dec->fmt_in.subs.dvb.i_id & 0xFFFF;
282 p_sys->i_ancillary_id = p_dec->fmt_in.subs.dvb.i_id >> 16;
283 p_sys->p_page = NULL;
284 p_sys->p_regions = NULL;
285 p_sys->p_objects = NULL;
286 p_sys->p_vout = NULL;
288 for( i = 0; i < 256; i++ ) p_sys->p_clut[i] = NULL;
290 es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'd','v','b','s' ) );
292 default_clut_init( p_dec );
297 /*****************************************************************************
299 *****************************************************************************/
300 static void Close( vlc_object_t *p_this )
302 decoder_t *p_dec = (decoder_t*) p_this;
303 decoder_sys_t *p_sys = p_dec->p_sys;
305 if( p_sys->p_vout && p_sys->p_vout->p_subpicture != NULL )
307 subpicture_t *p_subpic;
309 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
311 p_subpic = &p_sys->p_vout->p_subpicture[i_subpic];
312 if( p_subpic != NULL &&
313 ( p_subpic->i_status == RESERVED_SUBPICTURE ||
314 p_subpic->i_status == READY_SUBPICTURE ) )
316 vout_DestroySubPicture( p_sys->p_vout, p_subpic );
325 /*****************************************************************************
327 *****************************************************************************/
328 static void Decode( decoder_t *p_dec, block_t **pp_block )
330 decoder_sys_t *p_sys = p_dec->p_sys;
332 vout_thread_t *p_last_vout;
334 if( pp_block == NULL || *pp_block == NULL )
341 p_sys->i_pts = p_block->i_pts;
342 if( p_sys->i_pts <= 0 )
344 msg_Warn( p_dec, "non dated subtitle" );
345 block_Release( p_block );
349 bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
351 if( bs_read( &p_sys->bs, 8 ) != 0x20 ) /* Data identifier */
353 msg_Dbg( p_dec, "invalid data identifier" );
354 block_Release( p_block );
358 if( bs_read( &p_sys->bs, 8 ) != 0x20 && 0 ) /* Subtitle stream id */
360 msg_Dbg( p_dec, "invalid subtitle stream id" );
361 block_Release( p_block );
365 while( bs_show( &p_sys->bs, 8 ) == 0x0f ) /* Sync byte */
367 decode_segment( p_dec, &p_sys->bs );
370 if( bs_read( &p_sys->bs, 8 ) != 0xff ) /* End marker */
372 msg_Warn( p_dec, "end marker not found (corrupted subtitle ?)" );
373 block_Release( p_block );
377 p_last_vout = p_sys->p_vout;
378 if( ( p_sys->p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT,
381 if( p_last_vout != p_sys->p_vout )
383 p_sys->i_subpic_channel =
384 vout_RegisterOSDChannel( p_sys->p_vout );
387 /* Check if the page is to be displayed */
388 if( p_sys->p_page ) render( p_dec, p_sys->p_vout );
390 vlc_object_release( p_sys->p_vout );
393 else if( p_sys->p_page ) render( p_dec, NULL );
396 block_Release( p_block );
399 /* following functions are local */
401 /*****************************************************************************
402 * default_clut_init: default clut as defined in EN 300-743 section 10
403 *****************************************************************************/
404 static void default_clut_init( decoder_t *p_dec )
406 decoder_sys_t *p_sys = p_dec->p_sys;
409 #define RGB_TO_Y(r, g, b) ((int16_t) 77 * r + 150 * g + 29 * b) / 256;
410 #define RGB_TO_U(r, g, b) ((int16_t) -44 * r - 87 * g + 131 * b) / 256;
411 #define RGB_TO_V(r, g, b) ((int16_t) 131 * r - 110 * g - 21 * b) / 256;
414 for( i = 0; i < 4; i++ )
416 uint8_t R = 0, G = 0, B = 0, T = 0;
418 if( !(i & 0x2) && !(i & 0x1) ) T = 0xFF;
419 else if( !(i & 0x2) && (i & 0x1) ) R = G = B = 0xFF;
420 else if( (i & 0x2) && !(i & 0x1) ) R = G = B = 0;
421 else R = G = B = 0x7F;
423 p_sys->default_clut.c_2b[i].Y = RGB_TO_Y(R,G,B);
424 p_sys->default_clut.c_2b[i].Cr = RGB_TO_U(R,G,B);
425 p_sys->default_clut.c_2b[i].Cb = RGB_TO_V(R,G,B);
426 p_sys->default_clut.c_2b[i].T = T;
429 /* 16 entries CLUT */
430 for( i = 0; i < 16; i++ )
432 uint8_t R = 0, G = 0, B = 0, T = 0;
436 if( !(i & 0x4) && !(i & 0x2) && !(i & 0x1) )
442 R = (i & 0x1) ? 0xFF : 0;
443 G = (i & 0x2) ? 0xFF : 0;
444 B = (i & 0x4) ? 0xFF : 0;
449 R = (i & 0x1) ? 0x7F : 0;
450 G = (i & 0x2) ? 0x7F : 0;
451 B = (i & 0x4) ? 0x7F : 0;
454 p_sys->default_clut.c_4b[i].Y = RGB_TO_Y(R,G,B);
455 p_sys->default_clut.c_4b[i].Cr = RGB_TO_U(R,G,B);
456 p_sys->default_clut.c_4b[i].Cb = RGB_TO_V(R,G,B);
457 p_sys->default_clut.c_4b[i].T = T;
460 /* 256 entries CLUT (TODO) */
461 memset( p_sys->default_clut.c_8b, 0xFF, 256 * sizeof(dvbsub_color_t) );
464 static void decode_segment( decoder_t *p_dec, bs_t *s )
466 decoder_sys_t *p_sys = p_dec->p_sys;
471 /* sync_byte (already checked) */
475 i_type = bs_read( s, 8 );
478 i_page_id = bs_read( s, 16 );
481 i_size = bs_show( s, 16 );
483 if( i_page_id != p_sys->i_id && i_page_id != p_sys->i_ancillary_id )
486 msg_Dbg( p_dec, "subtitle skipped (page id: %i)", i_page_id );
488 bs_skip( s, 8 * ( 2 + i_size ) );
493 if( i_page_id == p_sys->i_id )
494 msg_Dbg( p_dec, "segment (id: %i)", i_page_id );
496 msg_Dbg( p_dec, "ancillary segment (id: %i)", i_page_id );
501 case DVBSUB_ST_PAGE_COMPOSITION:
503 msg_Dbg( p_dec, "decode_page_composition" );
505 decode_page_composition( p_dec, s );
508 case DVBSUB_ST_REGION_COMPOSITION:
510 msg_Dbg( p_dec, "decode_region_composition" );
512 decode_region_composition( p_dec, s );
515 case DVBSUB_ST_CLUT_DEFINITION:
517 msg_Dbg( p_dec, "decode_clut" );
519 decode_clut( p_dec, s );
522 case DVBSUB_ST_OBJECT_DATA:
524 msg_Dbg( p_dec, "decode_object" );
526 decode_object( p_dec, s );
529 case DVBSUB_ST_ENDOFDISPLAY:
531 msg_Dbg( p_dec, "end of display" );
533 bs_skip( s, 8 * ( 2 + i_size ) );
536 case DVBSUB_ST_STUFFING:
538 msg_Dbg( p_dec, "skip stuffing" );
540 bs_skip( s, 8 * ( 2 + i_size ) );
544 msg_Warn( p_dec, "unsupported segment type: (%04x)", i_type );
545 bs_skip( s, 8 * ( 2 + i_size ) );
550 static void decode_clut( decoder_t *p_dec, bs_t *s )
552 decoder_sys_t *p_sys = p_dec->p_sys;
553 uint16_t i_segment_length;
554 uint16_t i_processed_length;
555 dvbsub_clut_t *p_clut;
557 uint8_t i_version_number;
559 i_segment_length = bs_read( s, 16 );
560 i_clut_id = bs_read( s, 8 );
561 i_version_number = bs_read( s, 4 );
563 /* Check that this id doesn't not already exist with the same version
564 * number and allocate memory if necessary */
565 if( p_sys->p_clut[i_clut_id] != NULL &&
566 p_sys->p_clut[i_clut_id]->i_version_number == i_version_number )
569 bs_skip( s, 8 * i_segment_length - 12 );
573 if( !p_sys->p_clut[i_clut_id] )
575 p_sys->p_clut[i_clut_id] = malloc( sizeof(dvbsub_clut_t) );
578 p_clut = p_sys->p_clut[i_clut_id];
580 /* We don't have this version of the CLUT: Parse it */
581 p_clut->i_version_number = i_version_number;
582 bs_skip( s, 4 ); /* Reserved bits */
583 i_processed_length = 2;
584 while( i_processed_length < i_segment_length )
586 uint8_t y, cb, cr, t;
590 i_id = bs_read( s, 8 );
591 i_type = bs_read( s, 3 );
595 if( bs_read( s, 1 ) )
598 cr = bs_read( s, 8 );
599 cb = bs_read( s, 8 );
601 i_processed_length += 6;
606 cr = bs_read( s, 4 );
607 cb = bs_read( s, 4 );
609 i_processed_length += 4;
612 /* According to EN 300-743 section 7.2.3 note 1, type should
613 * not have more than 1 bit set to one, but some strams don't
614 * respect this note. */
618 p_clut->c_2b[i_id].Y = y;
619 p_clut->c_2b[i_id].Cr = cr;
620 p_clut->c_2b[i_id].Cb = cb;
621 p_clut->c_2b[i_id].T = t;
625 p_clut->c_4b[i_id].Y = y;
626 p_clut->c_4b[i_id].Cr = cr;
627 p_clut->c_4b[i_id].Cb = cb;
628 p_clut->c_4b[i_id].T = t;
632 p_clut->c_8b[i_id].Y = y;
633 p_clut->c_8b[i_id].Cr = cr;
634 p_clut->c_8b[i_id].Cb = cb;
635 p_clut->c_8b[i_id].T = t;
640 static void decode_page_composition( decoder_t *p_dec, bs_t *s )
642 decoder_sys_t *p_sys = p_dec->p_sys;
643 unsigned int i_version_number;
644 unsigned int i_state;
645 unsigned int i_segment_length;
649 /* A page is composed by one or more region */
651 i_segment_length = bs_read( s, 16 );
652 i_timeout = bs_read( s, 8 );
653 i_version_number = bs_read( s, 4 );
654 i_state = bs_read( s, 2 );
655 bs_skip( s, 2 ); /* Reserved */
657 if( i_state == DVBSUB_PCS_STATE_CHANGE )
659 /* End of an epoch, reset decoder buffer */
661 msg_Dbg( p_dec, "page composition mode change" );
665 else if( !p_sys->p_page && i_state != DVBSUB_PCS_STATE_ACQUISITION )
667 /* Not a full PCS, we need to wait for one */
671 if( i_state == DVBSUB_PCS_STATE_ACQUISITION )
673 /* Make sure we clean up regularly our objects list.
674 * Is it the best place to do this ? */
675 free_objects( p_dec );
678 /* Check version number */
680 p_sys->p_page->i_version_number == i_version_number )
682 bs_skip( s, 8 * (i_segment_length - 2) );
685 else if( p_sys->p_page )
687 if( p_sys->p_page->i_region_defs )
688 free( p_sys->p_page->p_region_defs );
689 p_sys->p_page->i_region_defs = 0;
694 /* Allocate a new page */
695 p_sys->p_page = malloc( sizeof(dvbsub_page_t) );
698 p_sys->p_page->i_version_number = i_version_number;
699 p_sys->p_page->i_timeout = i_timeout;
701 /* Number of regions */
702 p_sys->p_page->i_region_defs = (i_segment_length - 2) / 6;
704 if( p_sys->p_page->i_region_defs == 0 ) return;
706 p_sys->p_page->p_region_defs =
707 malloc( p_sys->p_page->i_region_defs * sizeof(dvbsub_region_t) );
708 for( i = 0; i < p_sys->p_page->i_region_defs; i++ )
710 p_sys->p_page->p_region_defs[i].i_id = bs_read( s, 8 );
711 bs_skip( s, 8 ); /* Reserved */
712 p_sys->p_page->p_region_defs[i].i_x = bs_read( s, 16 );
713 p_sys->p_page->p_region_defs[i].i_y = bs_read( s, 16 );
716 msg_Dbg( p_dec, "page_composition, region %i (%i,%i)",
717 i, p_sys->p_page->p_region_defs[i].i_x,
718 p_sys->p_page->p_region_defs[i].i_y );
723 static void decode_region_composition( decoder_t *p_dec, bs_t *s )
725 decoder_sys_t *p_sys = p_dec->p_sys;
726 dvbsub_region_t *p_region, **pp_region = &p_sys->p_regions;
727 int i_segment_length;
728 int i_processed_length;
730 int i_version_number;
732 i_segment_length = bs_read( s, 16 );
733 i_region_id = bs_read( s, 8 );
734 i_version_number = bs_read( s, 4 );
736 /* Check if we already have this region */
737 for( p_region = p_sys->p_regions; p_region != NULL;
738 p_region = p_region->p_next )
740 pp_region = &p_region->p_next;
741 if( p_region->i_id == i_region_id ) break;
744 /* Check version number */
746 p_region->i_version_number == i_version_number )
748 bs_skip( s, 8 * (i_segment_length - 1) - 4 );
753 if( p_region->i_object_defs )
754 free( p_region->p_object_defs );
760 msg_Dbg( p_dec, "new region: %i", i_region_id );
762 p_region = *pp_region = malloc( sizeof(dvbsub_region_t) );
763 p_region->p_next = NULL;
766 /* Region attributes */
767 p_region->i_id = i_region_id;
768 p_region->i_version_number = i_version_number;
769 p_region->b_fill = bs_read( s, 1 );
770 bs_skip( s, 3 ); /* Reserved */
771 p_region->i_width = bs_read( s, 16 );
772 p_region->i_height = bs_read( s, 16 );
773 p_region->i_level_comp = bs_read( s, 3 );
774 p_region->i_depth = bs_read( s, 3 );
775 bs_skip( s, 2 ); /* Reserved */
776 p_region->i_clut = bs_read( s, 8 );
777 p_region->i_8bp_code = bs_read( s, 8 );
778 p_region->i_4bp_code = bs_read( s, 4 );
779 p_region->i_2bp_code = bs_read( s, 2 );
780 bs_skip( s, 2 ); /* Reserved */
781 p_region->p_object_defs = NULL;
782 p_region->i_object_defs = 0;
784 /* List of objects in the region */
785 i_processed_length = 10;
786 while( i_processed_length < i_segment_length )
788 dvbsub_objectdef_t *p_obj;
790 /* We create a new object */
791 p_region->i_object_defs++;
792 p_region->p_object_defs =
793 realloc( p_region->p_object_defs,
794 sizeof(dvbsub_objectdef_t) * p_region->i_object_defs );
796 /* We parse object properties */
797 p_obj = &p_region->p_object_defs[p_region->i_object_defs - 1];
798 p_obj->i_id = bs_read( s, 16 );
799 p_obj->i_type = bs_read( s, 2 );
800 p_obj->i_provider = bs_read( s, 2 );
801 p_obj->i_x = bs_read( s, 12 );
802 bs_skip( s, 4 ); /* Reserved */
803 p_obj->i_y = bs_read( s, 12 );
805 i_processed_length += 6;
807 if( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ||
808 p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING )
810 p_obj->i_fg_pc = bs_read( s, 8 );
811 p_obj->i_bg_pc = bs_read( s, 8 );
812 i_processed_length += 2;
817 static dvbsub_image_t *dvbsub_parse_pdata( decoder_t *, bs_t *, uint16_t );
818 static uint16_t dvbsub_pdata2bpp( bs_t *, uint16_t *, dvbsub_image_t * );
819 static uint16_t dvbsub_pdata4bpp( bs_t *, uint16_t *, dvbsub_image_t * );
820 static uint16_t dvbsub_pdata8bpp( bs_t *, uint16_t *, dvbsub_image_t * );
822 static void decode_object( decoder_t *p_dec, bs_t *s )
824 decoder_sys_t *p_sys = p_dec->p_sys;
825 dvbsub_object_t *p_obj, **pp_obj = &p_sys->p_objects;
826 int i_segment_length;
827 int i_version_number;
831 i_segment_length = bs_read( s, 16 );
832 i_obj_id = bs_read( s, 16 );
833 i_version_number = bs_read( s, 4 );
834 i_coding_method = bs_read( s, 2 );
836 if( i_coding_method )
838 /* TODO: DVB subtitling as characters */
839 msg_Dbg( p_dec, "DVB subtitling as characters is not handled!" );
840 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
844 /* Check if we already have this region */
845 for( p_obj = p_sys->p_objects; p_obj != NULL; p_obj = p_obj->p_next )
847 pp_obj = &p_obj->p_next;
848 if( p_obj->i_id == i_obj_id ) break;
851 /* Check version number */
852 if( p_obj && p_obj->i_version_number == i_version_number )
854 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
859 /* Clean structure */
865 msg_Dbg( p_dec, "new object: %i", i_obj_id );
867 p_obj = *pp_obj = malloc( sizeof(dvbsub_object_t) );
868 p_obj->p_next = NULL;
871 p_obj->i_id = i_obj_id;
872 p_obj->i_version_number = i_version_number;
873 p_obj->i_coding_method = i_coding_method;
874 p_obj->b_non_modify_color = bs_read( s, 1 );
875 bs_skip( s, 1 ); /* Reserved */
877 if( p_obj->i_coding_method == 0x00 )
879 uint16_t i_topfield_length;
880 uint16_t i_bottomfield_length;
882 i_topfield_length = bs_read( s, 16 );
883 i_bottomfield_length = bs_read( s, 16 );
886 dvbsub_parse_pdata( p_dec, s, i_topfield_length );
888 dvbsub_parse_pdata( p_dec, s, i_bottomfield_length );
892 /* TODO: DVB subtitling as characters */
896 static dvbsub_image_t* dvbsub_parse_pdata( decoder_t *p_dec, bs_t *s,
899 dvbsub_image_t* p_image;
900 uint16_t i_processed_length = 0;
901 uint16_t i_lines = 0;
902 uint16_t i_cols_last = 0;
904 p_image = malloc( sizeof(dvbsub_image_t) );
905 p_image->p_last = NULL;
907 memset( p_image->i_cols, 0, 576 * sizeof(uint16_t) );
909 /* Let's parse it a first time to determine the size of the buffer */
910 while( i_processed_length < length)
912 i_processed_length++;
914 switch( bs_read( s, 8 ) )
917 i_processed_length +=
918 dvbsub_pdata2bpp( s, &p_image->i_cols[i_lines], p_image );
921 i_processed_length +=
922 dvbsub_pdata4bpp( s, &p_image->i_cols[i_lines], p_image );
925 i_processed_length +=
926 dvbsub_pdata8bpp( s, &p_image->i_cols[i_lines], p_image );
931 /* We don't use map tables */
934 i_lines++; /* End of line code */
939 p_image->i_rows = i_lines;
940 p_image->i_cols[i_lines] = i_cols_last;
942 /* Check word-aligned bits */
943 if( bs_show( s, 8 ) == 0x00 )
951 static void add_rle_code( dvbsub_image_t *p, uint16_t num, uint8_t color,
954 if( p->p_last != NULL )
956 p->p_last->p_next = malloc( sizeof(dvbsub_rle_t) );
957 p->p_last = p->p_last->p_next;
961 p->p_codes = malloc( sizeof(dvbsub_rle_t) );
962 p->p_last = p->p_codes;
964 p->p_last->i_num = num;
966 p->p_last->i_color_code = color;
967 p->p_last->i_bpp = i_bpp;
968 p->p_last->p_next = NULL;
971 static uint16_t dvbsub_pdata2bpp( bs_t *s, uint16_t* p,
972 dvbsub_image_t* p_image )
974 uint16_t i_processed = 0;
975 vlc_bool_t b_stop = 0;
976 uint16_t i_count = 0;
982 if( (i_color = bs_read( s, 2 )) != 0x00 )
987 add_rle_code( p_image, 1, i_color, 2 );
992 if( bs_read( s, 1 ) == 0x00 ) // Switch1
994 i_count = 3 + bs_read( s, 3 );
996 i_color = bs_read( s, 2 );
997 add_rle_code( p_image, i_count, i_color, 2 );
1003 if( bs_read( s, 1 ) == 0x00 ) //Switch2
1006 switch( bs_read( s, 2 ) ) //Switch3
1012 add_rle_code( p_image, 2, 0, 2 );
1015 i_count = 12 + bs_read( s, 4 );
1016 i_color = bs_read( s, 2 );
1019 add_rle_code( p_image, i_count, i_color, 2 );
1022 i_count = 29 + bs_read( s, 8 );
1023 i_color = bs_read( s, 2 );
1026 add_rle_code( p_image, i_count, i_color, 2 );
1038 return ( i_processed + 7 ) / 8 ;
1041 static uint16_t dvbsub_pdata4bpp( bs_t *s, uint16_t* p,
1042 dvbsub_image_t* p_image )
1044 uint16_t i_processed = 0;
1045 vlc_bool_t b_stop = 0;
1046 uint16_t i_count = 0;
1047 uint8_t i_color = 0;
1051 if( (i_color = bs_read( s, 4 )) != 0x00 )
1057 add_rle_code( p_image, 1, i_color, 4 );
1061 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1063 if( bs_show( s, 3 ) != 0x00 )
1065 i_count = 2 + bs_read( s, 3 );
1067 add_rle_code( p_image, i_count, 0x00, 4 );
1078 if( bs_read( s, 1 ) == 0x00) //Switch2
1080 i_count = 4 + bs_read( s, 2 );
1081 i_color = bs_read( s, 4 );
1084 add_rle_code( p_image, i_count, i_color, 4 );
1088 switch ( bs_read( s, 2 ) ) //Switch3
1093 add_rle_code( p_image, 1, 0x00, 4 );
1098 add_rle_code( p_image, 2, 0x00, 4 );
1101 i_count = 9 + bs_read( s, 4 );
1102 i_color = bs_read( s, 4 );
1105 add_rle_code( p_image, i_count, i_color, 4 );
1108 i_count= 25 + bs_read( s, 8 );
1109 i_color = bs_read( s, 4 );
1112 add_rle_code( p_image, i_count, i_color, 4 );
1122 return ( i_processed + 7 ) / 8 ;
1125 static uint16_t dvbsub_pdata8bpp( bs_t *s, uint16_t* p,
1126 dvbsub_image_t* p_image )
1128 uint16_t i_processed = 0;
1129 vlc_bool_t b_stop = 0;
1130 uint16_t i_count = 0;
1131 uint8_t i_color = 0;
1136 if( (i_color = bs_read( s, 8 )) != 0x00 )
1141 add_rle_code( p_image, 1, i_color, 8 );
1146 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1148 if( bs_show( s, 7 ) != 0x00 )
1150 i_count = bs_read( s, 7 );
1152 add_rle_code( p_image, i_count, 0x00, 8 );
1163 i_count = bs_read( s, 7 );
1165 i_color = bs_read( s, 8 );
1166 add_rle_code( p_image, i_count, i_color, 8 );
1174 return ( i_processed + 7 ) / 8 ;
1177 static dvbsub_image_t *dup_image( dvbsub_image_t *p_i )
1179 dvbsub_image_t *p_image = malloc( sizeof(dvbsub_image_t) );
1180 dvbsub_rle_t *p_rle = p_i->p_codes;
1183 p_image->p_last = NULL;
1187 dvbsub_rle_t *p_last = p_image->p_last;
1188 p_image->p_last = malloc( sizeof(dvbsub_rle_t) );
1189 if( !p_last ) p_image->p_codes = p_image->p_last;
1190 if( p_last ) p_last->p_next = p_image->p_last;
1191 *p_image->p_last = *p_rle;
1192 p_rle = p_rle->p_next;
1198 static void free_image( dvbsub_image_t *p_i )
1201 dvbsub_rle_t *p2 = NULL;
1203 for( p1 = p_i->p_codes; p1 != NULL; p1 = p2 )
1213 static void free_spu( subpicture_t *p_spu )
1215 dvbsub_render_t *p_obj, *p_obj_next;
1217 for( p_obj = p_spu->p_sys->p_objects; p_obj != NULL; p_obj = p_obj_next )
1219 p_obj_next = p_obj->p_next;
1220 free_image( p_obj->p_rle_top );
1221 free_image( p_obj->p_rle_bot );
1224 free( p_spu->p_sys );
1225 p_spu->p_sys = NULL;
1228 static void free_objects( decoder_t *p_dec )
1230 decoder_sys_t *p_sys = p_dec->p_sys;
1231 dvbsub_object_t *p_obj, *p_obj_next;
1233 for( p_obj = p_sys->p_objects; p_obj != NULL; p_obj = p_obj_next )
1235 p_obj_next = p_obj->p_next;
1236 free_image( p_obj->topfield );
1237 free_image( p_obj->bottomfield );
1240 p_sys->p_objects = NULL;
1243 static void free_all( decoder_t *p_dec )
1245 decoder_sys_t *p_sys = p_dec->p_sys;
1246 dvbsub_region_t *p_reg, *p_reg_next;
1249 for( i = 0; i < 256; i++ )
1251 if( p_sys->p_clut[i] ) free( p_sys->p_clut[i] );
1252 p_sys->p_clut[i] = NULL;
1257 if( p_sys->p_page->i_region_defs )
1258 free( p_sys->p_page->p_region_defs );
1259 free( p_sys->p_page );
1260 p_sys->p_page = NULL;
1263 for( p_reg = p_sys->p_regions; p_reg != NULL; p_reg = p_reg_next )
1265 p_reg_next = p_reg->p_next;
1266 if( p_reg->i_object_defs ) free( p_reg->p_object_defs );
1269 p_sys->p_regions = NULL;
1271 free_objects( p_dec );
1274 static void RenderYUY2( vout_thread_t *p_vout, picture_t *p_pic,
1275 dvbsub_render_t *p_r )
1277 /* Common variables */
1283 dvbsub_image_t* p_im = p_r->p_rle_top;
1286 p_desty = p_pic->Y_PIXELS;
1287 //let's render the 1st frame
1288 for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1290 //if( p_c->y != 0 && p_c->t < 0x20)
1291 if( p_c->y != 0 && p_c->t < 0x20)
1295 //memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1296 // In YUY2 we have to set pixel per pixel
1297 for( i_cnt = 0; i_cnt < p_c->i_num; i_cnt+=2 )
1299 memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt, p_c->y, 1);
1300 //memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+1, p_c->cr, 1);
1301 //memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+2, p_c->y, 1);
1302 //memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+3, p_c->cb, 1);
1306 if(j >= p_im->i_cols[i])
1310 if( i>= p_im->i_rows) break;
1312 //idem for the second frame
1313 p_im = p_r->p_rle_bot; i=0; j=0;
1314 for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1316 if( p_c->y != 0 && p_c->t < 0x20)
1320 //memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1321 // In YUY2 we have to set pixel per pixel
1322 for( i_cnt = 0; i_cnt < p_c->i_num; i_cnt+=2 )
1324 memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt, p_c->y, 1);
1325 //memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+1, p_c->cr, 1);
1326 //memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+2, p_c->y, 1);
1327 //memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+3, p_c->cb, 1);
1331 if(j >= p_im->i_cols[i])
1335 if( i>= p_im->i_rows) break;
1339 static void RenderI42x( vout_thread_t *p_vout, picture_t *p_pic,
1340 dvbsub_render_t *p_r )
1342 /* Common variables */
1343 uint8_t *p_desty = p_pic->Y_PIXELS;
1344 uint8_t *p_destu = p_pic->U_PIXELS;
1345 uint8_t *p_destv = p_pic->V_PIXELS;
1346 dvbsub_image_t* p_im = p_r->p_rle_top;
1348 uint16_t i, j, x, y;
1349 int i_x_subsampling =
1350 p_vout->output.i_chroma == VLC_FOURCC('I','4','2','2') ? 1 : 2;
1352 /* Let's render the top field */
1353 p_im = p_r->p_rle_bot; i = 0; j = 0;
1354 for( p_c = p_im->p_codes; p_c->p_next != NULL; p_c = p_c->p_next )
1356 if( p_c->y != 0 && p_c->t != 0xFF )
1359 y = 2 * i + p_r->i_y;
1360 memset( p_desty + y * p_pic->Y_PITCH + x, p_c->y, p_c->i_num );
1362 memset( p_destu + y/2 * p_pic->U_PITCH + x/i_x_subsampling,
1363 p_c->cr, p_c->i_num/i_x_subsampling );
1364 memset( p_destv + y/2 * p_pic->V_PITCH + x/i_x_subsampling,
1365 p_c->cb, p_c->i_num/i_x_subsampling );
1369 if( j >= p_im->i_cols[i] )
1374 if( i >= p_im->i_rows) break;
1377 /* Idem for the bottom field */
1378 p_im = p_r->p_rle_bot; i = 0; j = 0;
1379 for( p_c = p_im->p_codes; p_c->p_next != NULL; p_c = p_c->p_next )
1381 if( p_c->y != 0 && p_c->t != 0xFF )
1384 y = 2*i + 1 + p_r->i_y;
1386 memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1388 /* No U or V (decimation) */
1392 if( j >= p_im->i_cols[i] )
1397 if( i >= p_im->i_rows ) break;
1401 static void RenderDVBSUB( vout_thread_t *p_vout, picture_t *p_pic,
1402 const subpicture_t *p_spu )
1404 dvbsub_render_t* p_render;
1406 if( p_spu->p_sys == NULL ) return;
1408 p_render = p_spu->p_sys->p_objects;
1412 switch( p_vout->output.i_chroma )
1414 /* I420 target, no scaling */
1415 case VLC_FOURCC('I','4','2','2'):
1416 case VLC_FOURCC('I','4','2','0'):
1417 case VLC_FOURCC('I','Y','U','V'):
1418 case VLC_FOURCC('Y','V','1','2'):
1419 /* As long as we just use Y info, I422 and YV12 are just equivalent
1420 * to I420. Remember to change it the day we'll take into account
1422 RenderI42x( p_vout, p_pic, p_render );
1425 /* RV16 target, scaling */
1426 case VLC_FOURCC('R','V','1','6'):
1427 msg_Err(p_vout, "unimplemented chroma: RV16");
1428 /* RenderRV16( p_vout, p_pic, p_spu ); */
1431 /* RV32 target, scaling */
1432 case VLC_FOURCC('R','V','2','4'):
1433 case VLC_FOURCC('R','V','3','2'):
1434 msg_Err(p_vout, "unimplemented chroma: RV32");
1435 /* RenderRV32( p_vout, p_pic, p_spu ); */
1438 /* NVidia overlay, no scaling */
1439 case VLC_FOURCC('Y','U','Y','2'):
1440 RenderYUY2( p_vout, p_pic, p_render );
1444 msg_Err( p_vout, "unknown chroma, can't render SPU" );
1448 p_render = p_render->p_next;
1452 static void render( decoder_t *p_dec, vout_thread_t *p_vout )
1454 decoder_sys_t *p_sys = p_dec->p_sys;
1455 dvbsub_render_t *p_render = NULL, *p_current, *p_last = NULL;
1456 dvbsub_clut_t *p_clut;
1458 int i, j = 0, i_timeout = 0;
1460 /* Allocate the subpicture internal data. */
1462 if( !p_vout ) p_sys->p_spu = malloc( sizeof(subpicture_t) );
1466 vout_CreateSubPicture( p_vout, p_sys->i_subpic_channel,
1467 MEMORY_SUBPICTURE );
1468 if( p_sys->p_spu == NULL ) return;
1470 /* Loop on region definitions */
1473 msg_Dbg( p_dec, "rendering %i regions", p_sys->p_page->i_region_defs );
1476 for( i = 0; p_sys->p_page && i < p_sys->p_page->i_region_defs; i++ )
1478 dvbsub_region_t *p_region;
1479 dvbsub_regiondef_t *p_regiondef;
1481 i_timeout = p_sys->p_page->i_timeout;
1483 p_regiondef = &p_sys->p_page->p_region_defs[i];
1486 msg_Dbg( p_dec, "rendering region %i (%i,%i)", i,
1487 p_regiondef->i_x, p_regiondef->i_y );
1490 /* Find associated region */
1491 for( p_region = p_sys->p_regions; p_region != NULL;
1492 p_region = p_region->p_next )
1494 if( p_regiondef->i_id == p_region->i_id ) break;
1499 msg_Err( p_dec, "no region founddddd!!!" );
1503 /* Loop on object definitions */
1504 for( j = 0; j < p_region->i_object_defs; j++ )
1506 dvbsub_object_t *p_object;
1507 dvbsub_objectdef_t *p_objectdef;
1509 p_objectdef = &p_region->p_object_defs[j];
1512 msg_Dbg( p_dec, "rendering object %i (%i,%i)", p_objectdef->i_id,
1513 p_objectdef->i_x, p_objectdef->i_y );
1516 /* Look for the right object */
1517 for( p_object = p_sys->p_objects; p_object != NULL;
1518 p_object = p_object->p_next )
1520 if( p_objectdef->i_id == p_object->i_id ) break;
1525 msg_Err( p_dec, "no object founddddd!!!" );
1529 /* Allocate the render structure */
1530 p_current = malloc( sizeof(dvbsub_render_t) );
1531 p_current->p_next = NULL;
1532 p_current->i_x = p_regiondef->i_x + p_objectdef->i_x;
1533 p_current->i_y = p_regiondef->i_y + p_objectdef->i_y;
1534 p_current->p_rle_top = dup_image( p_object->topfield );
1535 p_current->p_rle_bot = dup_image( p_object->bottomfield );
1537 if( !p_render ) p_render = p_current;
1538 if( p_last ) p_last->p_next = p_current;
1541 p_clut = p_sys->p_clut[p_region->i_clut];
1542 if( !p_clut ) p_clut = &p_sys->default_clut;
1544 /* Compute the color datas according to the appropriate CLUT */
1545 for( p_c = p_current->p_rle_top->p_codes;
1546 p_c->p_next != NULL; p_c = p_c->p_next )
1548 dvbsub_color_t *p_color = (p_c->i_bpp == 2) ? p_clut->c_2b :
1549 (p_c->i_bpp == 4) ? p_clut->c_4b : p_clut->c_8b;
1551 p_c->y = p_color[p_c->i_color_code].Y;
1552 p_c->cr = p_color[p_c->i_color_code].Cr;
1553 p_c->cb = p_color[p_c->i_color_code].Cb;
1554 p_c->t = p_color[p_c->i_color_code].T;
1556 for( p_c = p_current->p_rle_bot->p_codes; p_c->p_next != NULL;
1559 dvbsub_color_t *p_color = (p_c->i_bpp == 2) ? p_clut->c_2b :
1560 (p_c->i_bpp == 4) ? p_clut->c_4b : p_clut->c_8b;
1562 p_c->y = p_color[p_c->i_color_code].Y;
1563 p_c->cr = p_color[p_c->i_color_code].Cr;
1564 p_c->cb = p_color[p_c->i_color_code].Cb;
1565 p_c->t = p_color[p_c->i_color_code].T;
1570 /* Set the pf_render callback */
1571 p_sys->p_spu->pf_render = RenderDVBSUB;
1572 p_sys->p_spu->p_sys = malloc( sizeof(subpicture_sys_t) );
1573 p_sys->p_spu->p_sys->p_objects = p_render;
1574 p_sys->p_spu->pf_destroy = free_spu;
1575 p_sys->p_spu->i_start = p_sys->i_pts;
1576 p_sys->p_spu->i_stop = p_sys->p_spu->i_start + i_timeout * 1000000;
1577 p_sys->p_spu->b_ephemer = VLC_TRUE;
1582 free_spu( p_sys->p_spu );
1583 free( p_sys->p_spu );
1584 p_sys->p_spu = NULL;
1589 vout_DisplaySubPicture( p_vout, p_sys->p_spu );