1 /*****************************************************************************
2 * dvbsub.c : DVB subtitles decoder thread
3 *****************************************************************************
4 * Copyright (C) 2003 ANEVIA
5 * $Id: dvbsub.c,v 1.3 2003/11/16 21:07:30 gbazin 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 *****************************************************************************/
26 #include <stdlib.h> /* malloc(), free() */
27 #include <string.h> /* memcpy(), memset() */
30 #include <vlc/decoder.h>
34 // Wow, that's ugly but very usefull for a memory leak track
37 static long long unsigned int trox_malloc_nb = 0;
38 static long long unsigned int trox_free_nb = 0;
40 static void* trox_malloc (size_t size)
41 { ++trox_malloc_nb; return malloc (size); }
43 static void trox_free (void* ptr)
44 { ++trox_free_nb; free(ptr); return; }
46 static void trox_call ()
48 fprintf(stderr, "dvbbsub -- Memory usage: %llu mallocs %llu frees (%llu)\n",
51 trox_malloc_nb - trox_free_nb);
55 # define trox_malloc malloc
56 # define trox_free free
59 /****************************************************************************
61 ****************************************************************************
62 * Those structures refer closely to the ETSI 300 743 Object model
63 ****************************************************************************/
65 /* Storage of a RLE entry */
66 typedef struct dvbsub_rle_s
74 struct dvbsub_rle_s* p_next;
77 /* A subpicture image is a list of codes
78 * We need to store the length of each line since nothing specify in
79 * the standard that all lines shoudl have the same length
80 * WARNING: We assume here that a spu is less than 576 lines high */
86 dvbsub_rle_t* p_codes;
89 /* The object definition gives the position of the object in a region */
90 typedef struct dvbsub_objectdef_s
99 struct dvbsub_objectdef_s* p_next;
100 } dvbsub_objectdef_t;
102 /* The Region is an aera on the image
103 * with a list of the object definitions associated
105 typedef struct dvbsub_region_s
108 uint8_t i_version_number;
114 uint8_t i_level_comp;
120 dvbsub_objectdef_t* p_object;
123 /* The page defines the list of regions */
129 uint8_t i_version_number;
130 uint8_t i_regions_number;
131 dvbsub_region_t* regions;
134 /* An object is constituted of 2 images (for interleaving) */
135 typedef struct dvbsub_object_s
138 uint8_t i_version_number;
139 uint8_t i_coding_method;
140 vlc_bool_t b_non_modify_color;
141 dvbsub_image_t* topfield;
142 dvbsub_image_t* bottomfield;
143 struct dvbsub_object_s* p_next;
146 /* The entry in the palette CLUT */
159 uint8_t i_version_number;
160 dvbsub_color_t c_2b[0xff];
161 dvbsub_color_t c_4b[0xff];
162 dvbsub_color_t c_8b[0xff];
169 dvbsub_image_t* p_rle_top;
170 dvbsub_image_t* p_rle_bot;
175 dvbsub_clut_t* p_clut[0xff];
176 dvbsub_page_t* p_page;
177 dvbsub_object_t* p_objects;
178 subpicture_t* p_spu[16];
182 /* Thread properties and locks */
183 vlc_thread_t thread_id; /* Id for thread functions */
184 /* Input properties */
185 decoder_fifo_t* p_fifo; /* Stores the PES stream data */
186 bit_stream_t bit_stream; /* PES data at the bit level */
187 /* Output properties */
188 vout_thread_t* p_vout; /* Needed to create the subpictures */
190 struct subpicture_sys_t
193 void * p_data; /* rle datas are stored */
194 vlc_object_t* p_input; /* Link to the input */
195 vlc_bool_t b_obsolete;
197 // List of different SEGMENT TYPES
198 // According to EN 300-743, table 2
199 #define DVBSUB_ST_PAGE_COMPOSITION 0x10
200 #define DVBSUB_ST_REGION_COMPOSITION 0x11
201 #define DVBSUB_ST_CLUT_DEFINITION 0x12
202 #define DVBSUB_ST_OBJECT_DATA 0x13
203 #define DVBSUB_ST_ENDOFDISPLAY 0x80
204 #define DVBSUB_ST_STUFFING 0xff
205 // List of different OBJECT TYPES
206 // According to EN 300-743, table 6
207 #define DVBSUB_OT_BASIC_BITMAP 0x00
208 #define DVBSUB_OT_BASIC_CHAR 0x01
209 #define DVBSUB_OT_COMPOSITE_STRING 0x02
211 // According to EN 300-743, table 9
212 #define DVBSUB_DT_2BP_CODE_STRING 0x10
213 #define DVBSUB_DT_4BP_CODE_STRING 0x11
214 #define DVBSUB_DT_8BP_CODE_STRING 0x12
215 #define DVBSUB_DT_24_TABLE_DATA 0x20
216 #define DVBSUB_DT_28_TABLE_DATA 0x21
217 #define DVBSUB_DT_48_TABLE_DATA 0x22
218 #define DVBSUB_DT_END_LINE 0xf0
220 /*****************************************************************************
222 *****************************************************************************/
223 static int OpenDecoder ( vlc_object_t * );
224 static int RunDecoder ( decoder_fifo_t * );
225 static int InitThread ( dvbsub_thread_t * );
226 static void EndThread ( dvbsub_thread_t *i, dvbsub_all_t*);
227 static vout_thread_t *FindVout( dvbsub_thread_t * );
228 static void RenderI42x( vout_thread_t *, picture_t *, const subpicture_t *,
230 static void RenderYUY2( vout_thread_t *, picture_t *, const subpicture_t *,
232 static void dvbsub_clut_add_entry ( dvbsub_clut_t* clut, uint8_t type,
233 uint8_t id, uint8_t y, uint8_t cr,
234 uint8_t cb, uint8_t t);
235 static void dvbsub_add_objectdef_to_region ( dvbsub_objectdef_t* p_obj,
236 dvbsub_region_t* p_region );
237 static dvbsub_image_t* dvbsub_parse_pdata ( dvbsub_thread_t* ,uint16_t );
238 static uint16_t dvbsub_count0x11(dvbsub_thread_t* p_spudec,
240 dvbsub_image_t* p_image);
241 static void dvbsub_decode_segment ( dvbsub_thread_t *, dvbsub_all_t* );
242 static void dvbsub_decode_page_composition ( dvbsub_thread_t *, dvbsub_all_t* );
243 static void dvbsub_decode_region_composition ( dvbsub_thread_t*, dvbsub_all_t*);
244 static void dvbsub_decode_object ( dvbsub_thread_t*, dvbsub_all_t* );
245 static vlc_bool_t dvbsub_check_page ( dvbsub_all_t* );
246 static void dvbsub_render ( dvbsub_thread_t *p_spudec,dvbsub_all_t* );
247 static int dvbsub_parse ( dvbsub_thread_t *p_spudec, dvbsub_all_t* dvbsub );
248 static void dvbsub_decode_clut ( dvbsub_thread_t*, dvbsub_all_t*);
249 static void dvbsub_stop_display ( dvbsub_thread_t* p_dec, dvbsub_all_t* dvbsub);
251 static void free_image (dvbsub_image_t* p_i);
252 static void free_object (dvbsub_object_t* p_o);
253 static void free_regions (dvbsub_region_t* p_r, uint8_t nb);
254 static void free_objects (dvbsub_object_t* p_o);
255 static void free_clut ( dvbsub_clut_t* p_c);
256 static void free_page (dvbsub_page_t* p_p);
257 static void free_all ( dvbsub_all_t* p_a );
260 /*****************************************************************************
262 *****************************************************************************/
264 add_category_hint( N_("subtitles"), NULL, VLC_TRUE );
265 set_description( _("subtitles decoder") );
266 set_capability( "decoder", 50 );
267 set_callbacks( OpenDecoder, NULL );
269 /*****************************************************************************
270 * OpenDecoder: probe the decoder and return score
271 *****************************************************************************
272 * Tries to launch a decoder and return score so that the interface is able
274 *****************************************************************************/
275 static int OpenDecoder( vlc_object_t *p_this )
277 decoder_t *p_dec = (decoder_t*) p_this;
278 if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','v','b','s') )
282 p_dec->pf_run = RunDecoder;
285 /*****************************************************************************
286 * RunDecoder: this function is called just after the thread is created
287 *****************************************************************************/
288 static int RunDecoder( decoder_fifo_t * p_fifo )
290 dvbsub_thread_t * p_dvbsubdec;
291 // vout_thread_t * p_vout_backup = NULL;
294 /* Allocate the memory needed to store the thread's structure */
295 p_dvbsubdec = (dvbsub_thread_t *)trox_malloc( sizeof(dvbsub_thread_t) );
296 if ( p_dvbsubdec == NULL )
298 msg_Err( p_fifo, "out of memory" );
299 DecoderError( p_fifo );
303 * Initialize the thread properties
305 p_dvbsubdec->p_vout = NULL;
306 p_dvbsubdec->p_fifo = p_fifo;
308 * Initialize thread and free configuration
310 p_dvbsubdec->p_fifo->b_error = InitThread( p_dvbsubdec );
312 dvbsub.p_objects=NULL;
313 for(k=0; k<0xff; k++) dvbsub.p_clut[k] = NULL;
314 for(k=0; k<16; k++) dvbsub.p_spu[k] = NULL;
317 * Main loop - it is not executed if an error occured during
320 while( (!p_dvbsubdec->p_fifo->b_die) && (!p_dvbsubdec->p_fifo->b_error) )
322 dvbsub_parse( p_dvbsubdec, &dvbsub );
323 p_dvbsubdec->p_vout = FindVout( p_dvbsubdec );
324 if( p_dvbsubdec->p_vout )
326 // Check if the page is to be displayed
327 if(dvbsub_check_page(&dvbsub))
329 dvbsub_render(p_dvbsubdec, &dvbsub);
331 vlc_object_release( p_dvbsubdec->p_vout );
334 // Free all structures
335 //dvbsub.p_objects=NULL;
336 //for(k=0; k<16; k++)
337 // if(dvbsub.p_spu[k] != NULL)
338 // dvbsub.p_spu[k]->p_sys->b_obsolete = 1;
343 if( p_dvbsubdec->p_fifo->b_error )
345 DecoderError( p_dvbsubdec->p_fifo );
347 EndThread( p_dvbsubdec, &dvbsub );
351 EndThread( p_dvbsubdec, &dvbsub );
355 /* following functions are local */
356 /*****************************************************************************
357 * InitThread: initialize dvbsub decoder thread
358 *****************************************************************************
359 * This function is called from RunThread and performs the second step of the
360 * initialization. It returns 0 on success. Note that the thread's flag are not
361 * modified inside this function.
362 *****************************************************************************/
363 static int InitThread( dvbsub_thread_t *p_dvbsubdec )
366 /* Call InitBitstream anyway so p_spudec->bit_stream is in a known
367 * state before calling CloseBitstream */
368 i_ret = InitBitstream( &p_dvbsubdec->bit_stream, p_dvbsubdec->p_fifo,
370 /* Check for a video output */
371 p_dvbsubdec->p_vout = FindVout( p_dvbsubdec );
372 if( !p_dvbsubdec->p_vout )
376 /* It was just a check */
377 vlc_object_release( p_dvbsubdec->p_vout );
378 p_dvbsubdec->p_vout = NULL;
381 /*****************************************************************************
382 * FindVout: Find a vout or wait for one to be created.
383 *****************************************************************************/
384 static vout_thread_t *FindVout( dvbsub_thread_t *p_spudec )
386 vout_thread_t *p_vout = NULL;
387 /* Find an available video output */
390 if( p_spudec->p_fifo->b_die || p_spudec->p_fifo->b_error )
394 p_vout = vlc_object_find( p_spudec->p_fifo, VLC_OBJECT_VOUT,
400 msleep( VOUT_OUTMEM_SLEEP );
405 /*****************************************************************************
406 * EndThread: thread destruction
407 *****************************************************************************
408 * This function is called when the thread ends after a sucessful
410 *****************************************************************************/
411 static void EndThread( dvbsub_thread_t *p_dvbsubdec, dvbsub_all_t* p_dvbsub )
413 if( p_dvbsubdec->p_vout != NULL
414 && p_dvbsubdec->p_vout->p_subpicture != NULL )
416 subpicture_t * p_subpic;
418 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
420 p_subpic = &p_dvbsubdec->p_vout->p_subpicture[i_subpic];
421 if( p_subpic != NULL &&
422 ( ( p_subpic->i_status == RESERVED_SUBPICTURE )
423 || ( p_subpic->i_status == READY_SUBPICTURE ) ) )
425 vout_DestroySubPicture( p_dvbsubdec->p_vout, p_subpic );
429 CloseBitstream( &p_dvbsubdec->bit_stream );
430 trox_free( p_dvbsubdec );
435 static int dvbsub_parse ( dvbsub_thread_t *p_spudec,
436 dvbsub_all_t* dvbsub )
438 unsigned int data_identifier;
439 unsigned int subtitle_stream_id;
440 unsigned int nextbits;
441 uint32_t end_data_marker;
442 /* Re-align the buffer on an 8-bit boundary */
443 RealignBits( &p_spudec->bit_stream );
444 data_identifier = GetBits( &p_spudec->bit_stream, 8 );
445 subtitle_stream_id = GetBits( &p_spudec->bit_stream, 8 );
446 nextbits = ShowBits( &p_spudec->bit_stream, 8 );
447 while(nextbits == 0x0f )
449 dvbsub_decode_segment( p_spudec, dvbsub );
450 nextbits = ShowBits( &p_spudec->bit_stream, 8 );
452 end_data_marker = GetBits( &p_spudec->bit_stream, 8 );
458 static void dvbsub_decode_segment ( dvbsub_thread_t * p_spudec,
459 dvbsub_all_t * dvbsub )
461 unsigned int sync_byte;
462 unsigned int segment_type;
464 uint16_t segment_length;
466 sync_byte = GetBits( &p_spudec->bit_stream, 8 );
467 segment_type = GetBits( &p_spudec->bit_stream, 8 );
468 page_id = GetBits( &p_spudec->bit_stream, 16 );
469 segment_length = ShowBits( &p_spudec->bit_stream, 16 );
470 if( page_id != ((dvb_spuinfo_t*)p_spudec->p_fifo->p_spuinfo)->i_id )
472 //TODO should use GetChunk
473 for(k=0; k<segment_length+2; k++) GetBits( &p_spudec->bit_stream, 8 );
476 switch( segment_type )
478 case DVBSUB_ST_CLUT_DEFINITION:
479 dvbsub_decode_clut ( p_spudec, dvbsub );
481 case DVBSUB_ST_PAGE_COMPOSITION:
482 dvbsub_decode_page_composition ( p_spudec, dvbsub );
484 case DVBSUB_ST_REGION_COMPOSITION:
485 dvbsub_decode_region_composition ( p_spudec, dvbsub );
487 case DVBSUB_ST_OBJECT_DATA:
488 dvbsub_decode_object ( p_spudec, dvbsub );
490 case DVBSUB_ST_ENDOFDISPLAY:
491 dvbsub_stop_display ( p_spudec, dvbsub);
493 case DVBSUB_ST_STUFFING:
495 fprintf(stderr, "*** DVBSUB - Unsupported segment type ! (%04x)\n",
497 GetBits( &p_spudec->bit_stream, 16 );
498 for(k=0; k<segment_length; k++)
499 GetBits( &p_spudec->bit_stream, 8 );
506 static void dvbsub_decode_page_composition (dvbsub_thread_t *p_spudec,
507 dvbsub_all_t *dvbsub)
509 unsigned int i_version_number;
510 unsigned int i_state;
511 unsigned int i_segment_length;
514 i_segment_length = GetBits( &p_spudec->bit_stream, 16 );
515 //A page is composed by one or more region:
516 i_timeout = GetBits( &p_spudec->bit_stream, 8 );
517 i_version_number = GetBits( &p_spudec->bit_stream, 4 );
518 i_state = GetBits( &p_spudec->bit_stream, 2 );
519 // TODO We assume it is a new page (i_state)
520 if (dvbsub->p_page) free_page(dvbsub->p_page);
522 GetBits( &p_spudec->bit_stream, 2 ); /* Reserved */
523 //Allocate a new page
524 dvbsub->p_page = trox_malloc (sizeof(dvbsub_page_t));
525 dvbsub->p_page->i_timeout = i_timeout;
526 // Number of regions:
527 dvbsub->p_page->i_regions_number = (i_segment_length-2) / 6;
529 /* Special workaround for CAVENA encoders
530 * a page with no regions is sent instead of a 0x80 packet (End Of Display) */
531 if( dvbsub->p_page->i_regions_number == 0 )
533 dvbsub_stop_display(p_spudec, dvbsub );
535 /* /Special workaround */
537 dvbsub->p_page->regions =
538 trox_malloc(dvbsub->p_page->i_regions_number*sizeof(dvbsub_region_t));
539 for(k=0; k<dvbsub->p_page->i_regions_number ; k++)
541 dvbsub->p_page->regions[k].i_id = GetBits( &p_spudec->bit_stream, 8 );
542 GetBits( &p_spudec->bit_stream, 8 ); /* Reserved */
543 dvbsub->p_page->regions[k].i_x = GetBits( &p_spudec->bit_stream, 16 );
544 dvbsub->p_page->regions[k].i_y = GetBits( &p_spudec->bit_stream, 16 );
545 dvbsub->p_page->regions[k].p_object = NULL;
550 static void dvbsub_decode_region_composition (dvbsub_thread_t *p_spudec,
551 dvbsub_all_t *dvbsub)
553 unsigned int i_segment_length;
554 unsigned int i_processed_length;
555 unsigned int i_region_id;
556 dvbsub_region_t* p_region;
559 i_segment_length = GetBits( &p_spudec->bit_stream, 16 );
561 i_region_id = GetBits( &p_spudec->bit_stream, 8 );
562 for(k=0; k<dvbsub->p_page->i_regions_number; k++)
564 if ( dvbsub->p_page->regions[k].i_id == i_region_id )
565 p_region = &(dvbsub->p_page->regions[k]);
570 // The region has never been declared before
572 fprintf (stderr, "Decoding of undeclared region N/A...\n");
575 // Skip version number and fill flag
576 if (ShowBits( &p_spudec->bit_stream, 4 ) == p_region->i_version_number)
578 fprintf(stderr, "Skipping already known region N/A ...\n");
579 // TODO Skip the right number of bits
582 p_region->i_version_number = GetBits( &p_spudec->bit_stream, 4 );
583 p_region->b_fill = GetBits( &p_spudec->bit_stream, 1 );
584 GetBits( &p_spudec->bit_stream, 3 ); /* Reserved */
585 p_region->i_width = GetBits( &p_spudec->bit_stream, 16 );
586 p_region->i_height = GetBits( &p_spudec->bit_stream, 16 );
587 p_region->i_level_comp = GetBits( &p_spudec->bit_stream, 3 );
588 p_region->i_depth = GetBits( &p_spudec->bit_stream, 3 );
589 GetBits( &p_spudec->bit_stream, 2 ); /* Reserved */
590 p_region->i_clut = GetBits( &p_spudec->bit_stream, 8 );
591 p_region->i_8bp_code = GetBits( &p_spudec->bit_stream, 8 );
592 p_region->i_4bp_code = GetBits( &p_spudec->bit_stream, 4 );
593 p_region->i_2bp_code = GetBits( &p_spudec->bit_stream, 2 );
594 GetBits( &p_spudec->bit_stream, 2 ); /* Reserved */
595 // List of objects in the region:
596 // We already skipped 10 bytes
597 i_processed_length = 10;
598 while ( i_processed_length < i_segment_length )
600 // We create a new object
601 dvbsub_objectdef_t* p_obj;
602 p_obj = trox_malloc(sizeof(dvbsub_objectdef_t));
603 // We parse object properties
604 p_obj->i_id = GetBits( &p_spudec->bit_stream, 16 );
605 p_obj->i_type = GetBits( &p_spudec->bit_stream, 2 );
606 p_obj->i_provider = GetBits( &p_spudec->bit_stream, 2 );
607 p_obj->i_xoffset = GetBits( &p_spudec->bit_stream, 12 );
608 GetBits( &p_spudec->bit_stream, 4 ); /* Reserved */
609 p_obj->i_yoffset = GetBits( &p_spudec->bit_stream, 12 );
610 i_processed_length += 6;
611 if ( p_obj->i_type == DVBSUB_OT_BASIC_CHAR
612 || p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING )
614 p_obj->i_fg_pc = GetBits( &p_spudec->bit_stream, 8 );
615 p_obj->i_bg_pc = GetBits( &p_spudec->bit_stream, 8 );
616 i_processed_length += 2;
618 p_obj->p_next = NULL;
619 dvbsub_add_objectdef_to_region(p_obj, p_region);
624 static void dvbsub_decode_object (dvbsub_thread_t* p_spudec,
625 dvbsub_all_t* dvbsub)
627 dvbsub_object_t* p_obj;
628 dvbsub_object_t* p_o;
629 uint16_t i_segment_length;
630 uint16_t i_topfield_length;
631 uint16_t i_bottomfield_length;
633 p_obj = trox_malloc ( sizeof ( dvbsub_object_t ) );
635 i_segment_length = GetBits( &p_spudec->bit_stream, 16 );
636 p_obj->i_id = GetBits( &p_spudec->bit_stream, 16 );
637 p_obj->i_version_number = GetBits( &p_spudec->bit_stream, 4 );
638 // TODO Check we don't already have this object / this version
639 p_obj->i_coding_method = GetBits( &p_spudec->bit_stream, 2 );
640 p_obj->b_non_modify_color = GetBits( &p_spudec->bit_stream, 1 );
641 GetBits( &p_spudec->bit_stream, 1 ); /* Reserved */
642 if(p_obj->i_coding_method == 0x00)
644 i_topfield_length = GetBits( &p_spudec->bit_stream, 16 );
645 i_bottomfield_length = GetBits( &p_spudec->bit_stream, 16 );
646 p_obj->topfield = dvbsub_parse_pdata (p_spudec, i_topfield_length);
648 dvbsub_parse_pdata (p_spudec, i_bottomfield_length);
652 GetBits(&p_spudec->bit_stream, (i_segment_length -3) *8);
654 // DVB subtitling as characters
656 // Add this object to the list of the page
657 p_o = dvbsub->p_objects;
658 dvbsub->p_objects = p_obj;
663 static void dvbsub_stop_display ( dvbsub_thread_t* p_dec,
664 dvbsub_all_t* dvbsub)
668 for(j = 0; dvbsub->p_spu[j] != NULL; j++)
669 dvbsub->p_spu[j]->i_stop = p_dec->bit_stream.p_pes->i_pts;
673 static void dvbsub_decode_clut ( dvbsub_thread_t* p_dec,
674 dvbsub_all_t* dvbsub)
676 uint16_t i_segment_length;
677 uint16_t i_processed_length;
679 uint8_t i_entry_type;
682 uint8_t i_version_number;
687 i_segment_length = GetBits( &p_dec->bit_stream, 16 );
688 i_clut_id = GetBits( &p_dec->bit_stream, 8 );
689 i_version_number = GetBits( &p_dec->bit_stream, 4 );
690 // Check that this id doesn't not already exist
691 // with the same version number
692 // And allocate memory if necessary
693 if( dvbsub->p_clut[i_clut_id] != NULL)
695 if ( dvbsub->p_clut[i_clut_id]->i_version_number == i_version_number )
697 //TODO skip the right number of bits
702 memset(dvbsub->p_clut[i_clut_id], 0, sizeof(dvbsub_clut_t));
707 dvbsub->p_clut[i_clut_id] = trox_malloc(sizeof(dvbsub_clut_t));
709 clut = dvbsub->p_clut[i_clut_id];
710 /* We don't have this version of the CLUT:
712 clut->i_version_number = i_version_number;
713 GetBits( &p_dec->bit_stream, 4 ); /* Reserved bits */
714 i_processed_length=2;
715 while(i_processed_length<i_segment_length)
717 i_entry_id = GetBits( &p_dec->bit_stream, 8 );
718 i_entry_type = GetBits( &p_dec->bit_stream, 3 );
719 GetBits( &p_dec->bit_stream, 4 );
720 if ( GetBits( &p_dec->bit_stream, 1 )==0x01 )
722 y = GetBits( &p_dec->bit_stream, 8 );
723 cr = GetBits( &p_dec->bit_stream, 8 );
724 cb = GetBits( &p_dec->bit_stream, 8 );
725 t = GetBits( &p_dec->bit_stream, 8 );
726 i_processed_length += 6;
730 y = GetBits( &p_dec->bit_stream, 6 );
731 cr = GetBits( &p_dec->bit_stream, 4 );
732 cb = GetBits( &p_dec->bit_stream, 4 );
733 t = GetBits( &p_dec->bit_stream, 2 );
734 i_processed_length += 4;
736 dvbsub_clut_add_entry(clut, i_entry_type, i_entry_id, y, cr, cb, t);
741 static void dvbsub_clut_add_entry ( dvbsub_clut_t* clut, uint8_t type,
742 uint8_t id, uint8_t y, uint8_t cr,
743 uint8_t cb, uint8_t t)
745 /* According to EN 300-743 section 7.2.3 note 1, type should
746 * not have more than 1 bit set to one
747 But, some strams don't respect this note. */
750 clut->c_2b[id].Y = y;
751 clut->c_2b[id].Cr = cr;
752 clut->c_2b[id].Cb = cb;
753 clut->c_2b[id].T = t;
757 clut->c_4b[id].Y = y;
758 clut->c_4b[id].Cr = cr;
759 clut->c_4b[id].Cb = cb;
760 clut->c_4b[id].T = t;
764 clut->c_8b[id].Y = y;
765 clut->c_8b[id].Cr = cr;
766 clut->c_8b[id].Cb = cb;
767 clut->c_8b[id].T = t;
773 static void dvbsub_add_objectdef_to_region ( dvbsub_objectdef_t* p_obj,
774 dvbsub_region_t* p_region )
776 dvbsub_objectdef_t* p_o = p_region->p_object;
777 // Seek to the last non null element
780 for(; p_o->p_next!=NULL; p_o=p_o->p_next);
782 p_o->p_next->p_next = NULL;
786 p_region->p_object = p_obj;
787 p_region->p_object->p_next = NULL;
793 static dvbsub_image_t* dvbsub_parse_pdata ( dvbsub_thread_t* p_spudec,
796 dvbsub_image_t* p_image;
797 uint16_t i_processed_length=0;
799 uint16_t i_cols_last=0;
800 p_image = trox_malloc ( sizeof ( dvbsub_image_t) );
801 p_image->p_last=NULL;
802 memset(p_image->i_cols, 0, 576*sizeof(uint16_t));
803 /* Let's parse it a first time to determine the size of the buffer */
804 while (i_processed_length < length)
806 switch(GetBits( &p_spudec->bit_stream, 8 ))
809 fprintf(stderr, "0x10 N/A\n");
812 i_processed_length += 1 + dvbsub_count0x11(p_spudec,
813 &(p_image->i_cols[i_lines]),
817 fprintf(stderr, "0x12 N/A\n");
820 fprintf(stderr, "0x20 N/A\n");
823 fprintf(stderr, "0x21 N/A\n");
826 fprintf(stderr, "0x22 N/A\n");
829 i_processed_length++;
834 p_image->i_rows = i_lines;
835 p_image->i_cols[i_lines] = i_cols_last;
836 // Check word-aligned bits
837 if(ShowBits( &p_spudec->bit_stream, 8 )==0x00)
838 GetBits( &p_spudec->bit_stream, 8 );
844 static void add_rle_code (dvbsub_image_t* p, uint16_t num, uint8_t color)
846 if(p->p_last != NULL)
848 p->p_last->p_next = trox_malloc (sizeof (dvbsub_rle_t));
849 p->p_last = p->p_last->p_next;
853 p->p_codes = trox_malloc (sizeof (dvbsub_rle_t));
854 p->p_last = p->p_codes;
856 p->p_last->i_num = num;
857 p->p_last->i_color_code = color;
858 p->p_last->p_next = NULL;
863 static uint16_t dvbsub_count0x11(dvbsub_thread_t* p_spudec, uint16_t* p, dvbsub_image_t* p_image)
865 uint16_t i_processed=0;
867 uint16_t i_count = 0;
871 if ( (i_color = GetBits( &p_spudec->bit_stream, 4 )) != 0x00 )
875 // 1 pixel of color code '0000'
876 add_rle_code (p_image, 1, i_color );
880 if(GetBits( &p_spudec->bit_stream, 1 ) == 0x00) // Switch1
882 if( ShowBits( &p_spudec->bit_stream, 3 ) != 0x00 )
884 i_count = 2 + GetBits( &p_spudec->bit_stream, 3 );
886 add_rle_code (p_image, i_count, 0x00);
890 GetBits( &p_spudec->bit_stream, 3);
897 if(GetBits( &p_spudec->bit_stream, 1 ) == 0x00) //Switch2
899 i_count = 4 + GetBits( &p_spudec->bit_stream, 2 );
900 i_color = GetBits( &p_spudec->bit_stream, 4 );
903 add_rle_code(p_image, i_count, i_color);
907 switch ( GetBits( &p_spudec->bit_stream, 2 ) ) //Switch3
912 add_rle_code(p_image, 1, 0x00);
917 add_rle_code(p_image, 2, 0x00);
920 i_count = 9 + GetBits( &p_spudec->bit_stream, 4 );
921 i_color = GetBits( &p_spudec->bit_stream, 4 );
924 add_rle_code ( p_image, i_count, i_color );
927 i_count= 25 + GetBits( &p_spudec->bit_stream, 8 );
928 i_color = GetBits( &p_spudec->bit_stream, 4 );
931 add_rle_code ( p_image, i_count, i_color );
938 RealignBits ( &p_spudec->bit_stream );
939 return (i_processed+7)/8 ;
942 static vlc_bool_t dvbsub_check_page(dvbsub_all_t* dvbsub)
944 if(dvbsub->p_page != NULL)
946 if(dvbsub->p_objects != NULL)
952 static void free_image (dvbsub_image_t* p_i)
955 dvbsub_rle_t* p2=NULL;
957 for( p1 = p_i->p_codes; p1 != NULL; p1=p2)
967 static void free_object (dvbsub_object_t* p_o)
972 static void free_objectdefs ( dvbsub_objectdef_t* p_o)
974 dvbsub_objectdef_t* p1;
975 dvbsub_objectdef_t* p2=NULL;
977 for( p1 = p_o; p1 != NULL; p1=p2)
985 static void free_regions (dvbsub_region_t* p_r, uint8_t nb)
989 for (i = 0; i<nb; i++) free_objectdefs ( p_r[i].p_object );
994 static void free_objects (dvbsub_object_t* p_o)
997 dvbsub_object_t* p2=NULL;
999 for( p1 = p_o; p1 != NULL; p1=p2)
1002 free_image (p1->topfield);
1003 free_image (p1->bottomfield);
1008 static void free_clut ( dvbsub_clut_t* p_c) { trox_free(p_c); }
1010 static void free_page (dvbsub_page_t* p_p)
1012 free_regions (p_p->regions, p_p->i_regions_number);
1017 static void free_spu( subpicture_t *p_spu )
1021 free_image(((dvbsub_render_t *)p_spu->p_sys->p_data)->p_rle_top);
1022 free_image(((dvbsub_render_t *)p_spu->p_sys->p_data)->p_rle_bot);
1023 trox_free(p_spu->p_sys->p_data);
1024 trox_free( p_spu->p_sys );
1025 p_spu->p_sys = NULL;
1029 static void free_all ( dvbsub_all_t* p_a )
1033 for(i=0; i<0xff; i++) if (p_a->p_clut[i]) free_clut ( p_a->p_clut[i] );
1034 for(i=0; i<16; i++) if (p_a->p_spu[i]) free_spu ( p_a->p_spu[i] );
1035 if(p_a->p_page) free_page( p_a->p_page );
1036 free_objects (p_a->p_objects);
1040 static void dvbsub_RenderDVBSUB ( vout_thread_t *p_vout, picture_t *p_pic,
1041 const subpicture_t *p_spu, vlc_bool_t b_crop )
1043 // If we have changed the language on the fly,
1044 if(!p_spu->p_sys) return;
1046 if(p_spu->p_sys->b_obsolete) return;
1048 switch (p_vout->output.i_chroma)
1050 /* I420 target, no scaling */
1051 case VLC_FOURCC('I','4','2','2'):
1052 case VLC_FOURCC('I','4','2','0'):
1053 case VLC_FOURCC('I','Y','U','V'):
1054 case VLC_FOURCC('Y','V','1','2'):
1055 // As long as we just use Y info, I422 and YV12 are just equivalent
1056 // to I420. Remember to change it the day we'll take into account
1058 RenderI42x( p_vout, p_pic, p_spu, VLC_FALSE );
1060 /* RV16 target, scaling */
1061 case VLC_FOURCC('R','V','1','6'):
1062 fprintf(stderr, "Not implemented chroma ! RV16)\n");
1063 //RenderRV16( p_vout, p_pic, p_spu, p_spu->p_sys->b_crop );
1065 /* RV32 target, scaling */
1066 case VLC_FOURCC('R','V','2','4'):
1067 case VLC_FOURCC('R','V','3','2'):
1068 fprintf(stderr, "Not implemented chroma ! RV32 \n");
1069 //RenderRV32( p_vout, p_pic, p_spu, p_spu->p_sys->b_crop );
1071 /* NVidia overlay, no scaling */
1072 case VLC_FOURCC('Y','U','Y','2'):
1073 RenderYUY2( p_vout, p_pic, p_spu, VLC_FALSE );
1076 msg_Err( p_vout, "unknown chroma, can't render SPU" );
1082 static void RenderYUY2 ( vout_thread_t *p_vout, picture_t *p_pic,
1083 const subpicture_t *p_spu, vlc_bool_t b_crop )
1085 /* Common variables */
1091 dvbsub_render_t* p_r = ((dvbsub_render_t *)p_spu->p_sys->p_data);
1092 dvbsub_image_t* p_im = p_r->p_rle_top;
1095 p_desty = p_pic->Y_PIXELS;
1096 //let's render the 1st frame
1097 for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1099 // if( p_c->y != 0 && p_c->t < 0x20)
1100 if( p_c->y != 0 && p_c->t < 0x20)
1104 //memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1105 // In YUY2 we have to set pixel per pixel
1106 for( i_cnt = 0; i_cnt < p_c->i_num; i_cnt+=2 )
1108 memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt, p_c->y, 1);
1109 // memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+1, p_c->cr, 1);
1110 // memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+2, p_c->y, 1);
1111 // memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+3, p_c->cb, 1);
1115 if(j >= p_im->i_cols[i])
1119 if( i>= p_im->i_rows) break;
1121 //idem for the second frame
1122 p_im = p_r->p_rle_bot; i=0; j=0;
1123 for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1125 if( p_c->y != 0 && p_c->t < 0x20)
1129 //memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1130 // In YUY2 we have to set pixel per pixel
1131 for( i_cnt = 0; i_cnt < p_c->i_num; i_cnt+=2 )
1133 memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt, p_c->y, 1);
1134 // memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+1, p_c->cr, 1);
1135 // memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+2, p_c->y, 1);
1136 // memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+3, p_c->cb, 1);
1140 if(j >= p_im->i_cols[i])
1144 if( i>= p_im->i_rows) break;
1149 static void RenderI42x ( vout_thread_t *p_vout, picture_t *p_pic,
1150 const subpicture_t *p_spu, vlc_bool_t b_crop )
1152 /* Common variables */
1159 dvbsub_render_t* p_r = ((dvbsub_render_t *)p_spu->p_sys->p_data);
1160 dvbsub_image_t* p_im = p_r->p_rle_top;
1163 p_desty = p_pic->Y_PIXELS;
1164 p_destu = p_pic->U_PIXELS;
1165 p_destv = p_pic->V_PIXELS;
1166 //let's render the 1st frame
1167 for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1173 //memset(p_dest+ y*p_pic->U_PITCH*2 + x, p_c->cr, p_c->i_num);
1174 // memset(p_desty+ (y)*p_pic->Y_PITCH + x, p_c->cr, p_c->i_num);
1175 //memset(p_dest+ y*p_pic->V_PITCH*2 + x, p_c->cb, p_c->i_num);
1176 //memset(p_destu+ (y)*p_pic->Y_PITCH + x, p_c->cb, p_c->i_num);
1177 memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1178 // memset(p_desty+ 2*y*p_pic->U_PITCH + x, p_c->cr, p_c->i_num);
1179 // memset(p_desty+ 2*y*p_pic->V_PITCH + x, p_c->cb, p_c->i_num);
1182 if(j >= p_im->i_cols[i])
1186 if( i>= p_im->i_rows) break;
1188 //idem for the second frame
1189 p_im = p_r->p_rle_bot; i=0; j=0;
1190 for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1192 if( p_c->y != 0 && p_c->t < 0x20)
1196 // memset(p_desty+ y*p_pic->U_PITCH*2 + x, p_c->cr, p_c->i_num);
1197 // memset(p_desty+ y*p_pic->V_PITCH*2 + x, p_c->cb, p_c->i_num);
1198 memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1199 // memset(p_desty+ 2*y*p_pic->U_PITCH + x, p_c->cr, p_c->i_num);
1200 // memset(p_desty+ 2*y*p_pic->V_PITCH + x, p_c->cb, p_c->i_num);
1203 if(j >= p_im->i_cols[i])
1207 if( i>= p_im->i_rows) break;
1211 static void dvbsub_Destroy( subpicture_t *p_spu )
1216 static void dvbsub_render( dvbsub_thread_t *p_dec, dvbsub_all_t* dvbsub)
1218 dvbsub_region_t* p_region;
1219 dvbsub_objectdef_t* p_objectdef;
1220 dvbsub_object_t* p_o;
1221 dvbsub_object_t* p_object;
1222 dvbsub_object_t* p_object_old;
1223 dvbsub_render_t* p_render;
1227 /* loop on regions */
1228 for(i=0; i< dvbsub->p_page->i_regions_number; i++)
1230 p_region = &(dvbsub->p_page->regions[i]);
1231 /* loop on objects */
1232 for(p_objectdef = p_region->p_object;
1233 p_objectdef != NULL;
1234 p_objectdef = p_objectdef->p_next)
1236 /* Look for the right object */
1237 p_object = dvbsub->p_objects;
1238 while((p_object!=NULL) && (p_object->i_id != p_objectdef->i_id))
1240 p_object = p_object->p_next;
1244 fprintf(stderr, "Internal DvbSub decoder error\n");
1247 /* Allocate the render structure */
1248 p_render = trox_malloc(sizeof(dvbsub_render_t));
1249 p_render->i_x = p_region->i_x + p_objectdef->i_xoffset;
1250 p_render->i_y = p_region->i_y + p_objectdef->i_yoffset;
1251 p_render->p_rle_top = p_object->topfield;
1252 p_render->p_rle_bot = p_object->bottomfield;
1254 // if we did not recieved the CLUT yet
1255 if ( !dvbsub->p_clut[p_region->i_clut] ) return;
1257 /* Compute the color datas according to the appropriate CLUT */
1258 for(p_c=p_render->p_rle_top->p_codes;p_c->p_next!=NULL; p_c=p_c->p_next)
1260 //TODO We assume here we are working in 4bp
1261 p_c->y=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Y;
1262 p_c->cr=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Cr;
1263 p_c->cb=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Cb;
1264 p_c->t=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].T;
1266 for(p_c=p_render->p_rle_bot->p_codes;p_c->p_next!=NULL; p_c=p_c->p_next)
1268 //TODO We assume here we are working in 4bp
1269 p_c->y=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Y;
1270 p_c->cr=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Cr;
1271 p_c->cb=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Cb;
1272 p_c->t=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].T;
1276 /* Allocate the subpicture internal data. */
1277 dvbsub->p_spu[j] = vout_CreateSubPicture( p_dec->p_vout,
1278 MEMORY_SUBPICTURE );
1279 if( dvbsub->p_spu[j] == NULL )
1281 fprintf(stderr, "Unable to allocate memory ... skipping\n");
1284 /* Set the pf_render callback */
1285 dvbsub->p_spu[j]->pf_render = dvbsub_RenderDVBSUB;
1286 dvbsub->p_spu[j]->p_sys = trox_malloc( sizeof( subpicture_sys_t ));
1287 dvbsub->p_spu[j]->p_sys->p_data = p_render;
1288 dvbsub->p_spu[j]->p_sys->b_obsolete=0;
1289 dvbsub->p_spu[j]->pf_destroy = dvbsub_Destroy;
1290 dvbsub->p_spu[j]->i_start = p_dec->bit_stream.p_pes->i_pts;
1291 dvbsub->p_spu[j]->i_stop = dvbsub->p_spu[j]->i_start + dvbsub->p_page->i_timeout*1000000;
1292 dvbsub->p_spu[j]->b_ephemer = VLC_FALSE;
1294 // At this stage, we have all we need in p_render
1295 // We need to free the object
1296 //Remove this object from the list
1297 p_object_old = p_object;
1298 if(p_object == dvbsub->p_objects)
1299 dvbsub->p_objects = p_object->p_next;
1302 for(p_o = dvbsub->p_objects; p_o->p_next != p_object; p_o=p_o->p_next);
1303 p_o->p_next = p_object->p_next;
1305 free_object(p_object_old);
1307 vout_DisplaySubPicture (p_dec->p_vout, dvbsub->p_spu[j] );