1 /*****************************************************************************
2 * dvbsub.c : DVB subtitles decoder thread
3 *****************************************************************************
4 * Copyright (C) 2003 ANEVIA
5 * $Id: dvbsub.c,v 1.4 2003/11/22 23:39:14 fenrir Exp $
7 * Authors: Damien LUCAS <damien.lucas@anevia.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
28 #include <vlc/decoder.h>
32 // Wow, that's ugly but very usefull for a memory leak track
35 static long long unsigned int trox_malloc_nb = 0;
36 static long long unsigned int trox_free_nb = 0;
38 static void* trox_malloc (size_t size)
39 { ++trox_malloc_nb; return malloc (size); }
41 static void trox_free (void* ptr)
42 { ++trox_free_nb; free(ptr); return; }
44 static void trox_call ()
46 fprintf(stderr, "dvbbsub -- Memory usage: %llu mallocs %llu frees (%llu)\n",
49 trox_malloc_nb - trox_free_nb);
53 # define trox_malloc malloc
54 # define trox_free free
57 /****************************************************************************
59 ****************************************************************************
60 * Those structures refer closely to the ETSI 300 743 Object model
61 ****************************************************************************/
63 /* Storage of a RLE entry */
64 typedef struct dvbsub_rle_s
72 struct dvbsub_rle_s* p_next;
75 /* A subpicture image is a list of codes
76 * We need to store the length of each line since nothing specify in
77 * the standard that all lines shoudl have the same length
78 * WARNING: We assume here that a spu is less than 576 lines high */
84 dvbsub_rle_t* p_codes;
87 /* The object definition gives the position of the object in a region */
88 typedef struct dvbsub_objectdef_s
97 struct dvbsub_objectdef_s* p_next;
100 /* The Region is an aera on the image
101 * with a list of the object definitions associated
103 typedef struct dvbsub_region_s
106 uint8_t i_version_number;
112 uint8_t i_level_comp;
118 dvbsub_objectdef_t* p_object;
121 /* The page defines the list of regions */
127 uint8_t i_version_number;
128 uint8_t i_regions_number;
129 dvbsub_region_t* regions;
132 /* An object is constituted of 2 images (for interleaving) */
133 typedef struct dvbsub_object_s
136 uint8_t i_version_number;
137 uint8_t i_coding_method;
138 vlc_bool_t b_non_modify_color;
139 dvbsub_image_t* topfield;
140 dvbsub_image_t* bottomfield;
141 struct dvbsub_object_s* p_next;
144 /* The entry in the palette CLUT */
157 uint8_t i_version_number;
158 dvbsub_color_t c_2b[0xff];
159 dvbsub_color_t c_4b[0xff];
160 dvbsub_color_t c_8b[0xff];
167 dvbsub_image_t* p_rle_top;
168 dvbsub_image_t* p_rle_bot;
173 dvbsub_clut_t* p_clut[0xff];
174 dvbsub_page_t* p_page;
175 dvbsub_object_t* p_objects;
176 subpicture_t* p_spu[16];
180 /* Thread properties and locks */
181 vlc_thread_t thread_id; /* Id for thread functions */
182 /* Input properties */
183 decoder_fifo_t* p_fifo; /* Stores the PES stream data */
184 bit_stream_t bit_stream; /* PES data at the bit level */
185 /* Output properties */
186 vout_thread_t* p_vout; /* Needed to create the subpictures */
188 struct subpicture_sys_t
191 void * p_data; /* rle datas are stored */
192 vlc_object_t* p_input; /* Link to the input */
193 vlc_bool_t b_obsolete;
195 // List of different SEGMENT TYPES
196 // According to EN 300-743, table 2
197 #define DVBSUB_ST_PAGE_COMPOSITION 0x10
198 #define DVBSUB_ST_REGION_COMPOSITION 0x11
199 #define DVBSUB_ST_CLUT_DEFINITION 0x12
200 #define DVBSUB_ST_OBJECT_DATA 0x13
201 #define DVBSUB_ST_ENDOFDISPLAY 0x80
202 #define DVBSUB_ST_STUFFING 0xff
203 // List of different OBJECT TYPES
204 // According to EN 300-743, table 6
205 #define DVBSUB_OT_BASIC_BITMAP 0x00
206 #define DVBSUB_OT_BASIC_CHAR 0x01
207 #define DVBSUB_OT_COMPOSITE_STRING 0x02
209 // According to EN 300-743, table 9
210 #define DVBSUB_DT_2BP_CODE_STRING 0x10
211 #define DVBSUB_DT_4BP_CODE_STRING 0x11
212 #define DVBSUB_DT_8BP_CODE_STRING 0x12
213 #define DVBSUB_DT_24_TABLE_DATA 0x20
214 #define DVBSUB_DT_28_TABLE_DATA 0x21
215 #define DVBSUB_DT_48_TABLE_DATA 0x22
216 #define DVBSUB_DT_END_LINE 0xf0
218 /*****************************************************************************
220 *****************************************************************************/
221 static int OpenDecoder ( vlc_object_t * );
222 static int RunDecoder ( decoder_fifo_t * );
223 static int InitThread ( dvbsub_thread_t * );
224 static void EndThread ( dvbsub_thread_t *i, dvbsub_all_t*);
225 static vout_thread_t *FindVout( dvbsub_thread_t * );
226 static void RenderI42x( vout_thread_t *, picture_t *, const subpicture_t *,
228 static void RenderYUY2( vout_thread_t *, picture_t *, const subpicture_t *,
230 static void dvbsub_clut_add_entry ( dvbsub_clut_t* clut, uint8_t type,
231 uint8_t id, uint8_t y, uint8_t cr,
232 uint8_t cb, uint8_t t);
233 static void dvbsub_add_objectdef_to_region ( dvbsub_objectdef_t* p_obj,
234 dvbsub_region_t* p_region );
235 static dvbsub_image_t* dvbsub_parse_pdata ( dvbsub_thread_t* ,uint16_t );
236 static uint16_t dvbsub_count0x11(dvbsub_thread_t* p_spudec,
238 dvbsub_image_t* p_image);
239 static void dvbsub_decode_segment ( dvbsub_thread_t *, dvbsub_all_t* );
240 static void dvbsub_decode_page_composition ( dvbsub_thread_t *, dvbsub_all_t* );
241 static void dvbsub_decode_region_composition ( dvbsub_thread_t*, dvbsub_all_t*);
242 static void dvbsub_decode_object ( dvbsub_thread_t*, dvbsub_all_t* );
243 static vlc_bool_t dvbsub_check_page ( dvbsub_all_t* );
244 static void dvbsub_render ( dvbsub_thread_t *p_spudec,dvbsub_all_t* );
245 static int dvbsub_parse ( dvbsub_thread_t *p_spudec, dvbsub_all_t* dvbsub );
246 static void dvbsub_decode_clut ( dvbsub_thread_t*, dvbsub_all_t*);
247 static void dvbsub_stop_display ( dvbsub_thread_t* p_dec, dvbsub_all_t* dvbsub);
249 static void free_image (dvbsub_image_t* p_i);
250 static void free_object (dvbsub_object_t* p_o);
251 static void free_regions (dvbsub_region_t* p_r, uint8_t nb);
252 static void free_objects (dvbsub_object_t* p_o);
253 static void free_clut ( dvbsub_clut_t* p_c);
254 static void free_page (dvbsub_page_t* p_p);
255 static void free_all ( dvbsub_all_t* p_a );
258 /*****************************************************************************
260 *****************************************************************************/
262 add_category_hint( N_("subtitles"), NULL, VLC_TRUE );
263 set_description( _("subtitles decoder") );
264 set_capability( "decoder", 50 );
265 set_callbacks( OpenDecoder, NULL );
267 /*****************************************************************************
268 * OpenDecoder: probe the decoder and return score
269 *****************************************************************************
270 * Tries to launch a decoder and return score so that the interface is able
272 *****************************************************************************/
273 static int OpenDecoder( vlc_object_t *p_this )
275 decoder_t *p_dec = (decoder_t*) p_this;
276 if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','v','b','s') )
280 p_dec->pf_run = RunDecoder;
283 /*****************************************************************************
284 * RunDecoder: this function is called just after the thread is created
285 *****************************************************************************/
286 static int RunDecoder( decoder_fifo_t * p_fifo )
288 dvbsub_thread_t * p_dvbsubdec;
289 // vout_thread_t * p_vout_backup = NULL;
292 /* Allocate the memory needed to store the thread's structure */
293 p_dvbsubdec = (dvbsub_thread_t *)trox_malloc( sizeof(dvbsub_thread_t) );
294 if ( p_dvbsubdec == NULL )
296 msg_Err( p_fifo, "out of memory" );
297 DecoderError( p_fifo );
301 * Initialize the thread properties
303 p_dvbsubdec->p_vout = NULL;
304 p_dvbsubdec->p_fifo = p_fifo;
306 * Initialize thread and free configuration
308 p_dvbsubdec->p_fifo->b_error = InitThread( p_dvbsubdec );
310 dvbsub.p_objects=NULL;
311 for(k=0; k<0xff; k++) dvbsub.p_clut[k] = NULL;
312 for(k=0; k<16; k++) dvbsub.p_spu[k] = NULL;
315 * Main loop - it is not executed if an error occured during
318 while( (!p_dvbsubdec->p_fifo->b_die) && (!p_dvbsubdec->p_fifo->b_error) )
320 dvbsub_parse( p_dvbsubdec, &dvbsub );
321 p_dvbsubdec->p_vout = FindVout( p_dvbsubdec );
322 if( p_dvbsubdec->p_vout )
324 // Check if the page is to be displayed
325 if(dvbsub_check_page(&dvbsub))
327 dvbsub_render(p_dvbsubdec, &dvbsub);
329 vlc_object_release( p_dvbsubdec->p_vout );
332 // Free all structures
333 //dvbsub.p_objects=NULL;
334 //for(k=0; k<16; k++)
335 // if(dvbsub.p_spu[k] != NULL)
336 // dvbsub.p_spu[k]->p_sys->b_obsolete = 1;
341 if( p_dvbsubdec->p_fifo->b_error )
343 DecoderError( p_dvbsubdec->p_fifo );
345 EndThread( p_dvbsubdec, &dvbsub );
349 EndThread( p_dvbsubdec, &dvbsub );
353 /* following functions are local */
354 /*****************************************************************************
355 * InitThread: initialize dvbsub decoder thread
356 *****************************************************************************
357 * This function is called from RunThread and performs the second step of the
358 * initialization. It returns 0 on success. Note that the thread's flag are not
359 * modified inside this function.
360 *****************************************************************************/
361 static int InitThread( dvbsub_thread_t *p_dvbsubdec )
364 /* Call InitBitstream anyway so p_spudec->bit_stream is in a known
365 * state before calling CloseBitstream */
366 i_ret = InitBitstream( &p_dvbsubdec->bit_stream, p_dvbsubdec->p_fifo,
368 /* Check for a video output */
369 p_dvbsubdec->p_vout = FindVout( p_dvbsubdec );
370 if( !p_dvbsubdec->p_vout )
374 /* It was just a check */
375 vlc_object_release( p_dvbsubdec->p_vout );
376 p_dvbsubdec->p_vout = NULL;
379 /*****************************************************************************
380 * FindVout: Find a vout or wait for one to be created.
381 *****************************************************************************/
382 static vout_thread_t *FindVout( dvbsub_thread_t *p_spudec )
384 vout_thread_t *p_vout = NULL;
385 /* Find an available video output */
388 if( p_spudec->p_fifo->b_die || p_spudec->p_fifo->b_error )
392 p_vout = vlc_object_find( p_spudec->p_fifo, VLC_OBJECT_VOUT,
398 msleep( VOUT_OUTMEM_SLEEP );
403 /*****************************************************************************
404 * EndThread: thread destruction
405 *****************************************************************************
406 * This function is called when the thread ends after a sucessful
408 *****************************************************************************/
409 static void EndThread( dvbsub_thread_t *p_dvbsubdec, dvbsub_all_t* p_dvbsub )
411 if( p_dvbsubdec->p_vout != NULL
412 && p_dvbsubdec->p_vout->p_subpicture != NULL )
414 subpicture_t * p_subpic;
416 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
418 p_subpic = &p_dvbsubdec->p_vout->p_subpicture[i_subpic];
419 if( p_subpic != NULL &&
420 ( ( p_subpic->i_status == RESERVED_SUBPICTURE )
421 || ( p_subpic->i_status == READY_SUBPICTURE ) ) )
423 vout_DestroySubPicture( p_dvbsubdec->p_vout, p_subpic );
427 CloseBitstream( &p_dvbsubdec->bit_stream );
428 trox_free( p_dvbsubdec );
433 static int dvbsub_parse ( dvbsub_thread_t *p_spudec,
434 dvbsub_all_t* dvbsub )
436 unsigned int data_identifier;
437 unsigned int subtitle_stream_id;
438 unsigned int nextbits;
439 uint32_t end_data_marker;
440 /* Re-align the buffer on an 8-bit boundary */
441 RealignBits( &p_spudec->bit_stream );
442 data_identifier = GetBits( &p_spudec->bit_stream, 8 );
443 subtitle_stream_id = GetBits( &p_spudec->bit_stream, 8 );
444 nextbits = ShowBits( &p_spudec->bit_stream, 8 );
445 while(nextbits == 0x0f )
447 dvbsub_decode_segment( p_spudec, dvbsub );
448 nextbits = ShowBits( &p_spudec->bit_stream, 8 );
450 end_data_marker = GetBits( &p_spudec->bit_stream, 8 );
456 static void dvbsub_decode_segment ( dvbsub_thread_t * p_spudec,
457 dvbsub_all_t * dvbsub )
459 unsigned int sync_byte;
460 unsigned int segment_type;
462 uint16_t segment_length;
464 sync_byte = GetBits( &p_spudec->bit_stream, 8 );
465 segment_type = GetBits( &p_spudec->bit_stream, 8 );
466 page_id = GetBits( &p_spudec->bit_stream, 16 );
467 segment_length = ShowBits( &p_spudec->bit_stream, 16 );
468 if( page_id != ((dvb_spuinfo_t*)p_spudec->p_fifo->p_spuinfo)->i_id )
470 //TODO should use GetChunk
471 for(k=0; k<segment_length+2; k++) GetBits( &p_spudec->bit_stream, 8 );
474 switch( segment_type )
476 case DVBSUB_ST_CLUT_DEFINITION:
477 dvbsub_decode_clut ( p_spudec, dvbsub );
479 case DVBSUB_ST_PAGE_COMPOSITION:
480 dvbsub_decode_page_composition ( p_spudec, dvbsub );
482 case DVBSUB_ST_REGION_COMPOSITION:
483 dvbsub_decode_region_composition ( p_spudec, dvbsub );
485 case DVBSUB_ST_OBJECT_DATA:
486 dvbsub_decode_object ( p_spudec, dvbsub );
488 case DVBSUB_ST_ENDOFDISPLAY:
489 dvbsub_stop_display ( p_spudec, dvbsub);
491 case DVBSUB_ST_STUFFING:
493 fprintf(stderr, "*** DVBSUB - Unsupported segment type ! (%04x)\n",
495 GetBits( &p_spudec->bit_stream, 16 );
496 for(k=0; k<segment_length; k++)
497 GetBits( &p_spudec->bit_stream, 8 );
504 static void dvbsub_decode_page_composition (dvbsub_thread_t *p_spudec,
505 dvbsub_all_t *dvbsub)
507 unsigned int i_version_number;
508 unsigned int i_state;
509 unsigned int i_segment_length;
512 i_segment_length = GetBits( &p_spudec->bit_stream, 16 );
513 //A page is composed by one or more region:
514 i_timeout = GetBits( &p_spudec->bit_stream, 8 );
515 i_version_number = GetBits( &p_spudec->bit_stream, 4 );
516 i_state = GetBits( &p_spudec->bit_stream, 2 );
517 // TODO We assume it is a new page (i_state)
518 if (dvbsub->p_page) free_page(dvbsub->p_page);
520 GetBits( &p_spudec->bit_stream, 2 ); /* Reserved */
521 //Allocate a new page
522 dvbsub->p_page = trox_malloc (sizeof(dvbsub_page_t));
523 dvbsub->p_page->i_timeout = i_timeout;
524 // Number of regions:
525 dvbsub->p_page->i_regions_number = (i_segment_length-2) / 6;
527 /* Special workaround for CAVENA encoders
528 * a page with no regions is sent instead of a 0x80 packet (End Of Display) */
529 if( dvbsub->p_page->i_regions_number == 0 )
531 dvbsub_stop_display(p_spudec, dvbsub );
533 /* /Special workaround */
535 dvbsub->p_page->regions =
536 trox_malloc(dvbsub->p_page->i_regions_number*sizeof(dvbsub_region_t));
537 for(k=0; k<dvbsub->p_page->i_regions_number ; k++)
539 dvbsub->p_page->regions[k].i_id = GetBits( &p_spudec->bit_stream, 8 );
540 GetBits( &p_spudec->bit_stream, 8 ); /* Reserved */
541 dvbsub->p_page->regions[k].i_x = GetBits( &p_spudec->bit_stream, 16 );
542 dvbsub->p_page->regions[k].i_y = GetBits( &p_spudec->bit_stream, 16 );
543 dvbsub->p_page->regions[k].p_object = NULL;
548 static void dvbsub_decode_region_composition (dvbsub_thread_t *p_spudec,
549 dvbsub_all_t *dvbsub)
551 unsigned int i_segment_length;
552 unsigned int i_processed_length;
553 unsigned int i_region_id;
554 dvbsub_region_t* p_region;
557 i_segment_length = GetBits( &p_spudec->bit_stream, 16 );
559 i_region_id = GetBits( &p_spudec->bit_stream, 8 );
560 for(k=0; k<dvbsub->p_page->i_regions_number; k++)
562 if ( dvbsub->p_page->regions[k].i_id == i_region_id )
563 p_region = &(dvbsub->p_page->regions[k]);
568 // The region has never been declared before
570 fprintf (stderr, "Decoding of undeclared region N/A...\n");
573 // Skip version number and fill flag
574 if (ShowBits( &p_spudec->bit_stream, 4 ) == p_region->i_version_number)
576 fprintf(stderr, "Skipping already known region N/A ...\n");
577 // TODO Skip the right number of bits
580 p_region->i_version_number = GetBits( &p_spudec->bit_stream, 4 );
581 p_region->b_fill = GetBits( &p_spudec->bit_stream, 1 );
582 GetBits( &p_spudec->bit_stream, 3 ); /* Reserved */
583 p_region->i_width = GetBits( &p_spudec->bit_stream, 16 );
584 p_region->i_height = GetBits( &p_spudec->bit_stream, 16 );
585 p_region->i_level_comp = GetBits( &p_spudec->bit_stream, 3 );
586 p_region->i_depth = GetBits( &p_spudec->bit_stream, 3 );
587 GetBits( &p_spudec->bit_stream, 2 ); /* Reserved */
588 p_region->i_clut = GetBits( &p_spudec->bit_stream, 8 );
589 p_region->i_8bp_code = GetBits( &p_spudec->bit_stream, 8 );
590 p_region->i_4bp_code = GetBits( &p_spudec->bit_stream, 4 );
591 p_region->i_2bp_code = GetBits( &p_spudec->bit_stream, 2 );
592 GetBits( &p_spudec->bit_stream, 2 ); /* Reserved */
593 // List of objects in the region:
594 // We already skipped 10 bytes
595 i_processed_length = 10;
596 while ( i_processed_length < i_segment_length )
598 // We create a new object
599 dvbsub_objectdef_t* p_obj;
600 p_obj = trox_malloc(sizeof(dvbsub_objectdef_t));
601 // We parse object properties
602 p_obj->i_id = GetBits( &p_spudec->bit_stream, 16 );
603 p_obj->i_type = GetBits( &p_spudec->bit_stream, 2 );
604 p_obj->i_provider = GetBits( &p_spudec->bit_stream, 2 );
605 p_obj->i_xoffset = GetBits( &p_spudec->bit_stream, 12 );
606 GetBits( &p_spudec->bit_stream, 4 ); /* Reserved */
607 p_obj->i_yoffset = GetBits( &p_spudec->bit_stream, 12 );
608 i_processed_length += 6;
609 if ( p_obj->i_type == DVBSUB_OT_BASIC_CHAR
610 || p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING )
612 p_obj->i_fg_pc = GetBits( &p_spudec->bit_stream, 8 );
613 p_obj->i_bg_pc = GetBits( &p_spudec->bit_stream, 8 );
614 i_processed_length += 2;
616 p_obj->p_next = NULL;
617 dvbsub_add_objectdef_to_region(p_obj, p_region);
622 static void dvbsub_decode_object (dvbsub_thread_t* p_spudec,
623 dvbsub_all_t* dvbsub)
625 dvbsub_object_t* p_obj;
626 dvbsub_object_t* p_o;
627 uint16_t i_segment_length;
628 uint16_t i_topfield_length;
629 uint16_t i_bottomfield_length;
631 p_obj = trox_malloc ( sizeof ( dvbsub_object_t ) );
633 i_segment_length = GetBits( &p_spudec->bit_stream, 16 );
634 p_obj->i_id = GetBits( &p_spudec->bit_stream, 16 );
635 p_obj->i_version_number = GetBits( &p_spudec->bit_stream, 4 );
636 // TODO Check we don't already have this object / this version
637 p_obj->i_coding_method = GetBits( &p_spudec->bit_stream, 2 );
638 p_obj->b_non_modify_color = GetBits( &p_spudec->bit_stream, 1 );
639 GetBits( &p_spudec->bit_stream, 1 ); /* Reserved */
640 if(p_obj->i_coding_method == 0x00)
642 i_topfield_length = GetBits( &p_spudec->bit_stream, 16 );
643 i_bottomfield_length = GetBits( &p_spudec->bit_stream, 16 );
644 p_obj->topfield = dvbsub_parse_pdata (p_spudec, i_topfield_length);
646 dvbsub_parse_pdata (p_spudec, i_bottomfield_length);
650 GetBits(&p_spudec->bit_stream, (i_segment_length -3) *8);
652 // DVB subtitling as characters
654 // Add this object to the list of the page
655 p_o = dvbsub->p_objects;
656 dvbsub->p_objects = p_obj;
661 static void dvbsub_stop_display ( dvbsub_thread_t* p_dec,
662 dvbsub_all_t* dvbsub)
666 for(j = 0; dvbsub->p_spu[j] != NULL; j++)
667 dvbsub->p_spu[j]->i_stop = p_dec->bit_stream.p_pes->i_pts;
671 static void dvbsub_decode_clut ( dvbsub_thread_t* p_dec,
672 dvbsub_all_t* dvbsub)
674 uint16_t i_segment_length;
675 uint16_t i_processed_length;
677 uint8_t i_entry_type;
680 uint8_t i_version_number;
685 i_segment_length = GetBits( &p_dec->bit_stream, 16 );
686 i_clut_id = GetBits( &p_dec->bit_stream, 8 );
687 i_version_number = GetBits( &p_dec->bit_stream, 4 );
688 // Check that this id doesn't not already exist
689 // with the same version number
690 // And allocate memory if necessary
691 if( dvbsub->p_clut[i_clut_id] != NULL)
693 if ( dvbsub->p_clut[i_clut_id]->i_version_number == i_version_number )
695 //TODO skip the right number of bits
700 memset(dvbsub->p_clut[i_clut_id], 0, sizeof(dvbsub_clut_t));
705 dvbsub->p_clut[i_clut_id] = trox_malloc(sizeof(dvbsub_clut_t));
707 clut = dvbsub->p_clut[i_clut_id];
708 /* We don't have this version of the CLUT:
710 clut->i_version_number = i_version_number;
711 GetBits( &p_dec->bit_stream, 4 ); /* Reserved bits */
712 i_processed_length=2;
713 while(i_processed_length<i_segment_length)
715 i_entry_id = GetBits( &p_dec->bit_stream, 8 );
716 i_entry_type = GetBits( &p_dec->bit_stream, 3 );
717 GetBits( &p_dec->bit_stream, 4 );
718 if ( GetBits( &p_dec->bit_stream, 1 )==0x01 )
720 y = GetBits( &p_dec->bit_stream, 8 );
721 cr = GetBits( &p_dec->bit_stream, 8 );
722 cb = GetBits( &p_dec->bit_stream, 8 );
723 t = GetBits( &p_dec->bit_stream, 8 );
724 i_processed_length += 6;
728 y = GetBits( &p_dec->bit_stream, 6 );
729 cr = GetBits( &p_dec->bit_stream, 4 );
730 cb = GetBits( &p_dec->bit_stream, 4 );
731 t = GetBits( &p_dec->bit_stream, 2 );
732 i_processed_length += 4;
734 dvbsub_clut_add_entry(clut, i_entry_type, i_entry_id, y, cr, cb, t);
739 static void dvbsub_clut_add_entry ( dvbsub_clut_t* clut, uint8_t type,
740 uint8_t id, uint8_t y, uint8_t cr,
741 uint8_t cb, uint8_t t)
743 /* According to EN 300-743 section 7.2.3 note 1, type should
744 * not have more than 1 bit set to one
745 But, some strams don't respect this note. */
748 clut->c_2b[id].Y = y;
749 clut->c_2b[id].Cr = cr;
750 clut->c_2b[id].Cb = cb;
751 clut->c_2b[id].T = t;
755 clut->c_4b[id].Y = y;
756 clut->c_4b[id].Cr = cr;
757 clut->c_4b[id].Cb = cb;
758 clut->c_4b[id].T = t;
762 clut->c_8b[id].Y = y;
763 clut->c_8b[id].Cr = cr;
764 clut->c_8b[id].Cb = cb;
765 clut->c_8b[id].T = t;
771 static void dvbsub_add_objectdef_to_region ( dvbsub_objectdef_t* p_obj,
772 dvbsub_region_t* p_region )
774 dvbsub_objectdef_t* p_o = p_region->p_object;
775 // Seek to the last non null element
778 for(; p_o->p_next!=NULL; p_o=p_o->p_next);
780 p_o->p_next->p_next = NULL;
784 p_region->p_object = p_obj;
785 p_region->p_object->p_next = NULL;
791 static dvbsub_image_t* dvbsub_parse_pdata ( dvbsub_thread_t* p_spudec,
794 dvbsub_image_t* p_image;
795 uint16_t i_processed_length=0;
797 uint16_t i_cols_last=0;
798 p_image = trox_malloc ( sizeof ( dvbsub_image_t) );
799 p_image->p_last=NULL;
800 memset(p_image->i_cols, 0, 576*sizeof(uint16_t));
801 /* Let's parse it a first time to determine the size of the buffer */
802 while (i_processed_length < length)
804 switch(GetBits( &p_spudec->bit_stream, 8 ))
807 fprintf(stderr, "0x10 N/A\n");
810 i_processed_length += 1 + dvbsub_count0x11(p_spudec,
811 &(p_image->i_cols[i_lines]),
815 fprintf(stderr, "0x12 N/A\n");
818 fprintf(stderr, "0x20 N/A\n");
821 fprintf(stderr, "0x21 N/A\n");
824 fprintf(stderr, "0x22 N/A\n");
827 i_processed_length++;
832 p_image->i_rows = i_lines;
833 p_image->i_cols[i_lines] = i_cols_last;
834 // Check word-aligned bits
835 if(ShowBits( &p_spudec->bit_stream, 8 )==0x00)
836 GetBits( &p_spudec->bit_stream, 8 );
842 static void add_rle_code (dvbsub_image_t* p, uint16_t num, uint8_t color)
844 if(p->p_last != NULL)
846 p->p_last->p_next = trox_malloc (sizeof (dvbsub_rle_t));
847 p->p_last = p->p_last->p_next;
851 p->p_codes = trox_malloc (sizeof (dvbsub_rle_t));
852 p->p_last = p->p_codes;
854 p->p_last->i_num = num;
855 p->p_last->i_color_code = color;
856 p->p_last->p_next = NULL;
861 static uint16_t dvbsub_count0x11(dvbsub_thread_t* p_spudec, uint16_t* p, dvbsub_image_t* p_image)
863 uint16_t i_processed=0;
865 uint16_t i_count = 0;
869 if ( (i_color = GetBits( &p_spudec->bit_stream, 4 )) != 0x00 )
873 // 1 pixel of color code '0000'
874 add_rle_code (p_image, 1, i_color );
878 if(GetBits( &p_spudec->bit_stream, 1 ) == 0x00) // Switch1
880 if( ShowBits( &p_spudec->bit_stream, 3 ) != 0x00 )
882 i_count = 2 + GetBits( &p_spudec->bit_stream, 3 );
884 add_rle_code (p_image, i_count, 0x00);
888 GetBits( &p_spudec->bit_stream, 3);
895 if(GetBits( &p_spudec->bit_stream, 1 ) == 0x00) //Switch2
897 i_count = 4 + GetBits( &p_spudec->bit_stream, 2 );
898 i_color = GetBits( &p_spudec->bit_stream, 4 );
901 add_rle_code(p_image, i_count, i_color);
905 switch ( GetBits( &p_spudec->bit_stream, 2 ) ) //Switch3
910 add_rle_code(p_image, 1, 0x00);
915 add_rle_code(p_image, 2, 0x00);
918 i_count = 9 + GetBits( &p_spudec->bit_stream, 4 );
919 i_color = GetBits( &p_spudec->bit_stream, 4 );
922 add_rle_code ( p_image, i_count, i_color );
925 i_count= 25 + GetBits( &p_spudec->bit_stream, 8 );
926 i_color = GetBits( &p_spudec->bit_stream, 4 );
929 add_rle_code ( p_image, i_count, i_color );
936 RealignBits ( &p_spudec->bit_stream );
937 return (i_processed+7)/8 ;
940 static vlc_bool_t dvbsub_check_page(dvbsub_all_t* dvbsub)
942 if(dvbsub->p_page != NULL)
944 if(dvbsub->p_objects != NULL)
950 static void free_image (dvbsub_image_t* p_i)
953 dvbsub_rle_t* p2=NULL;
955 for( p1 = p_i->p_codes; p1 != NULL; p1=p2)
965 static void free_object (dvbsub_object_t* p_o)
970 static void free_objectdefs ( dvbsub_objectdef_t* p_o)
972 dvbsub_objectdef_t* p1;
973 dvbsub_objectdef_t* p2=NULL;
975 for( p1 = p_o; p1 != NULL; p1=p2)
983 static void free_regions (dvbsub_region_t* p_r, uint8_t nb)
987 for (i = 0; i<nb; i++) free_objectdefs ( p_r[i].p_object );
992 static void free_objects (dvbsub_object_t* p_o)
995 dvbsub_object_t* p2=NULL;
997 for( p1 = p_o; p1 != NULL; p1=p2)
1000 free_image (p1->topfield);
1001 free_image (p1->bottomfield);
1006 static void free_clut ( dvbsub_clut_t* p_c) { trox_free(p_c); }
1008 static void free_page (dvbsub_page_t* p_p)
1010 free_regions (p_p->regions, p_p->i_regions_number);
1015 static void free_spu( subpicture_t *p_spu )
1019 free_image(((dvbsub_render_t *)p_spu->p_sys->p_data)->p_rle_top);
1020 free_image(((dvbsub_render_t *)p_spu->p_sys->p_data)->p_rle_bot);
1021 trox_free(p_spu->p_sys->p_data);
1022 trox_free( p_spu->p_sys );
1023 p_spu->p_sys = NULL;
1027 static void free_all ( dvbsub_all_t* p_a )
1031 for(i=0; i<0xff; i++) if (p_a->p_clut[i]) free_clut ( p_a->p_clut[i] );
1032 for(i=0; i<16; i++) if (p_a->p_spu[i]) free_spu ( p_a->p_spu[i] );
1033 if(p_a->p_page) free_page( p_a->p_page );
1034 free_objects (p_a->p_objects);
1038 static void dvbsub_RenderDVBSUB ( vout_thread_t *p_vout, picture_t *p_pic,
1039 const subpicture_t *p_spu, vlc_bool_t b_crop )
1041 // If we have changed the language on the fly,
1042 if(!p_spu->p_sys) return;
1044 if(p_spu->p_sys->b_obsolete) return;
1046 switch (p_vout->output.i_chroma)
1048 /* I420 target, no scaling */
1049 case VLC_FOURCC('I','4','2','2'):
1050 case VLC_FOURCC('I','4','2','0'):
1051 case VLC_FOURCC('I','Y','U','V'):
1052 case VLC_FOURCC('Y','V','1','2'):
1053 // As long as we just use Y info, I422 and YV12 are just equivalent
1054 // to I420. Remember to change it the day we'll take into account
1056 RenderI42x( p_vout, p_pic, p_spu, VLC_FALSE );
1058 /* RV16 target, scaling */
1059 case VLC_FOURCC('R','V','1','6'):
1060 fprintf(stderr, "Not implemented chroma ! RV16)\n");
1061 //RenderRV16( p_vout, p_pic, p_spu, p_spu->p_sys->b_crop );
1063 /* RV32 target, scaling */
1064 case VLC_FOURCC('R','V','2','4'):
1065 case VLC_FOURCC('R','V','3','2'):
1066 fprintf(stderr, "Not implemented chroma ! RV32 \n");
1067 //RenderRV32( p_vout, p_pic, p_spu, p_spu->p_sys->b_crop );
1069 /* NVidia overlay, no scaling */
1070 case VLC_FOURCC('Y','U','Y','2'):
1071 RenderYUY2( p_vout, p_pic, p_spu, VLC_FALSE );
1074 msg_Err( p_vout, "unknown chroma, can't render SPU" );
1080 static void RenderYUY2 ( vout_thread_t *p_vout, picture_t *p_pic,
1081 const subpicture_t *p_spu, vlc_bool_t b_crop )
1083 /* Common variables */
1089 dvbsub_render_t* p_r = ((dvbsub_render_t *)p_spu->p_sys->p_data);
1090 dvbsub_image_t* p_im = p_r->p_rle_top;
1093 p_desty = p_pic->Y_PIXELS;
1094 //let's render the 1st frame
1095 for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1097 // if( p_c->y != 0 && p_c->t < 0x20)
1098 if( p_c->y != 0 && p_c->t < 0x20)
1102 //memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1103 // In YUY2 we have to set pixel per pixel
1104 for( i_cnt = 0; i_cnt < p_c->i_num; i_cnt+=2 )
1106 memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt, p_c->y, 1);
1107 // memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+1, p_c->cr, 1);
1108 // memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+2, p_c->y, 1);
1109 // memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+3, p_c->cb, 1);
1113 if(j >= p_im->i_cols[i])
1117 if( i>= p_im->i_rows) break;
1119 //idem for the second frame
1120 p_im = p_r->p_rle_bot; i=0; j=0;
1121 for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1123 if( p_c->y != 0 && p_c->t < 0x20)
1127 //memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1128 // In YUY2 we have to set pixel per pixel
1129 for( i_cnt = 0; i_cnt < p_c->i_num; i_cnt+=2 )
1131 memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt, p_c->y, 1);
1132 // memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+1, p_c->cr, 1);
1133 // memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+2, p_c->y, 1);
1134 // memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+3, p_c->cb, 1);
1138 if(j >= p_im->i_cols[i])
1142 if( i>= p_im->i_rows) break;
1147 static void RenderI42x ( vout_thread_t *p_vout, picture_t *p_pic,
1148 const subpicture_t *p_spu, vlc_bool_t b_crop )
1150 /* Common variables */
1157 dvbsub_render_t* p_r = ((dvbsub_render_t *)p_spu->p_sys->p_data);
1158 dvbsub_image_t* p_im = p_r->p_rle_top;
1161 p_desty = p_pic->Y_PIXELS;
1162 p_destu = p_pic->U_PIXELS;
1163 p_destv = p_pic->V_PIXELS;
1164 //let's render the 1st frame
1165 for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1171 //memset(p_dest+ y*p_pic->U_PITCH*2 + x, p_c->cr, p_c->i_num);
1172 // memset(p_desty+ (y)*p_pic->Y_PITCH + x, p_c->cr, p_c->i_num);
1173 //memset(p_dest+ y*p_pic->V_PITCH*2 + x, p_c->cb, p_c->i_num);
1174 //memset(p_destu+ (y)*p_pic->Y_PITCH + x, p_c->cb, p_c->i_num);
1175 memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1176 // memset(p_desty+ 2*y*p_pic->U_PITCH + x, p_c->cr, p_c->i_num);
1177 // memset(p_desty+ 2*y*p_pic->V_PITCH + x, p_c->cb, p_c->i_num);
1180 if(j >= p_im->i_cols[i])
1184 if( i>= p_im->i_rows) break;
1186 //idem for the second frame
1187 p_im = p_r->p_rle_bot; i=0; j=0;
1188 for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1190 if( p_c->y != 0 && p_c->t < 0x20)
1194 // memset(p_desty+ y*p_pic->U_PITCH*2 + x, p_c->cr, p_c->i_num);
1195 // memset(p_desty+ y*p_pic->V_PITCH*2 + x, p_c->cb, p_c->i_num);
1196 memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1197 // memset(p_desty+ 2*y*p_pic->U_PITCH + x, p_c->cr, p_c->i_num);
1198 // memset(p_desty+ 2*y*p_pic->V_PITCH + x, p_c->cb, p_c->i_num);
1201 if(j >= p_im->i_cols[i])
1205 if( i>= p_im->i_rows) break;
1209 static void dvbsub_Destroy( subpicture_t *p_spu )
1214 static void dvbsub_render( dvbsub_thread_t *p_dec, dvbsub_all_t* dvbsub)
1216 dvbsub_region_t* p_region;
1217 dvbsub_objectdef_t* p_objectdef;
1218 dvbsub_object_t* p_o;
1219 dvbsub_object_t* p_object;
1220 dvbsub_object_t* p_object_old;
1221 dvbsub_render_t* p_render;
1225 /* loop on regions */
1226 for(i=0; i< dvbsub->p_page->i_regions_number; i++)
1228 p_region = &(dvbsub->p_page->regions[i]);
1229 /* loop on objects */
1230 for(p_objectdef = p_region->p_object;
1231 p_objectdef != NULL;
1232 p_objectdef = p_objectdef->p_next)
1234 /* Look for the right object */
1235 p_object = dvbsub->p_objects;
1236 while((p_object!=NULL) && (p_object->i_id != p_objectdef->i_id))
1238 p_object = p_object->p_next;
1242 fprintf(stderr, "Internal DvbSub decoder error\n");
1245 /* Allocate the render structure */
1246 p_render = trox_malloc(sizeof(dvbsub_render_t));
1247 p_render->i_x = p_region->i_x + p_objectdef->i_xoffset;
1248 p_render->i_y = p_region->i_y + p_objectdef->i_yoffset;
1249 p_render->p_rle_top = p_object->topfield;
1250 p_render->p_rle_bot = p_object->bottomfield;
1252 // if we did not recieved the CLUT yet
1253 if ( !dvbsub->p_clut[p_region->i_clut] ) return;
1255 /* Compute the color datas according to the appropriate CLUT */
1256 for(p_c=p_render->p_rle_top->p_codes;p_c->p_next!=NULL; p_c=p_c->p_next)
1258 //TODO We assume here we are working in 4bp
1259 p_c->y=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Y;
1260 p_c->cr=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Cr;
1261 p_c->cb=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Cb;
1262 p_c->t=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].T;
1264 for(p_c=p_render->p_rle_bot->p_codes;p_c->p_next!=NULL; p_c=p_c->p_next)
1266 //TODO We assume here we are working in 4bp
1267 p_c->y=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Y;
1268 p_c->cr=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Cr;
1269 p_c->cb=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Cb;
1270 p_c->t=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].T;
1274 /* Allocate the subpicture internal data. */
1275 dvbsub->p_spu[j] = vout_CreateSubPicture( p_dec->p_vout,
1276 MEMORY_SUBPICTURE );
1277 if( dvbsub->p_spu[j] == NULL )
1279 fprintf(stderr, "Unable to allocate memory ... skipping\n");
1282 /* Set the pf_render callback */
1283 dvbsub->p_spu[j]->pf_render = dvbsub_RenderDVBSUB;
1284 dvbsub->p_spu[j]->p_sys = trox_malloc( sizeof( subpicture_sys_t ));
1285 dvbsub->p_spu[j]->p_sys->p_data = p_render;
1286 dvbsub->p_spu[j]->p_sys->b_obsolete=0;
1287 dvbsub->p_spu[j]->pf_destroy = dvbsub_Destroy;
1288 dvbsub->p_spu[j]->i_start = p_dec->bit_stream.p_pes->i_pts;
1289 dvbsub->p_spu[j]->i_stop = dvbsub->p_spu[j]->i_start + dvbsub->p_page->i_timeout*1000000;
1290 dvbsub->p_spu[j]->b_ephemer = VLC_FALSE;
1292 // At this stage, we have all we need in p_render
1293 // We need to free the object
1294 //Remove this object from the list
1295 p_object_old = p_object;
1296 if(p_object == dvbsub->p_objects)
1297 dvbsub->p_objects = p_object->p_next;
1300 for(p_o = dvbsub->p_objects; p_o->p_next != p_object; p_o=p_o->p_next);
1301 p_o->p_next = p_object->p_next;
1303 free_object(p_object_old);
1305 vout_DisplaySubPicture (p_dec->p_vout, dvbsub->p_spu[j] );