]> git.sesse.net Git - vlc/blob - modules/codec/dvbsub.c
* src/video_output/vout_subpictures.c : New OSD channels
[vlc] / modules / codec / dvbsub.c
1 /*****************************************************************************
2  * dvbsub.c : DVB subtitles decoder thread
3  *****************************************************************************
4  * Copyright (C) 2003 ANEVIA
5  * Copyright (C) 2003-2004 VideoLAN
6  * $Id$
7  *
8  * Authors: Damien LUCAS <damien.lucas@anevia.com>
9  *          Laurent Aimar <fenrir@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc/vout.h>
30 #include <vlc/decoder.h>
31
32 #include "vlc_bits.h"
33
34 /*****************************************************************************
35  * Module descriptor.
36  *****************************************************************************/
37 static int  Open ( vlc_object_t *p_this );
38 static void Close( vlc_object_t *p_this );
39
40 vlc_module_begin();
41     set_description( _("DVB subtitles decoder") );
42     set_capability( "decoder", 50 );
43     set_callbacks( Open, Close );
44 vlc_module_end();
45
46 /****************************************************************************
47  * Local structures
48  ****************************************************************************
49  * Those structures refer closely to the ETSI 300 743 Object model
50  ****************************************************************************/
51
52 /* Storage of a RLE entry */
53 typedef struct dvbsub_rle_s
54 {
55     uint16_t                 i_num;
56     uint8_t                 i_color_code;
57     uint8_t                 y;
58     uint8_t                 cr;
59     uint8_t                 cb;
60     uint8_t                 t;
61     struct dvbsub_rle_s*    p_next;
62 } dvbsub_rle_t;
63
64 /* A subpicture image is a list of codes
65  * We need to store the length of each line since nothing specify in
66  * the standard that all lines shoudl have the same length
67  * WARNING: We assume here that a spu is less than 576 lines high */
68 typedef struct
69 {
70     uint16_t                        i_rows;
71     uint16_t                        i_cols[576];
72     dvbsub_rle_t*                   p_last;
73     dvbsub_rle_t*                   p_codes;
74
75 } dvbsub_image_t;
76
77 /* The object definition gives the position of the object in a region */
78 typedef struct dvbsub_objectdef_s
79 {
80     uint16_t                    i_id;
81     uint8_t                     i_type;
82     uint8_t                     i_provider;
83     uint16_t                    i_xoffset;
84     uint16_t                    i_yoffset;
85     uint8_t                     i_fg_pc;
86     uint8_t                     i_bg_pc;
87     struct dvbsub_objectdef_s*  p_next;
88
89 } dvbsub_objectdef_t;
90
91 /* The Region is an aera on the image
92  * with a list of the object definitions associated
93  * and a CLUT */
94 typedef struct dvbsub_region_s
95 {
96     uint8_t                 i_id;
97     uint8_t                 i_version_number;
98     vlc_bool_t              b_fill;
99     uint16_t                i_x;
100     uint16_t                i_y;
101     uint16_t                i_width;
102     uint16_t                i_height;
103     uint8_t                 i_level_comp;
104     uint8_t                 i_depth;
105     uint8_t                 i_clut;
106     uint8_t                 i_8bp_code;
107     uint8_t                 i_4bp_code;
108     uint8_t                 i_2bp_code;
109     dvbsub_objectdef_t*     p_object;
110
111 } dvbsub_region_t;
112
113 /* The page defines the list of regions */
114 typedef struct
115 {
116     uint16_t              i_id;
117     uint8_t               i_timeout;
118     uint8_t               i_state;
119     uint8_t               i_version_number;
120     uint8_t               i_regions_number;
121     dvbsub_region_t*      regions;
122
123 } dvbsub_page_t;
124
125 /* An object is constituted of 2 images (for interleaving) */
126 typedef struct dvbsub_object_s
127 {
128     uint16_t                i_id;
129     uint8_t                 i_version_number;
130     uint8_t                 i_coding_method;
131     vlc_bool_t              b_non_modify_color;
132     dvbsub_image_t*         topfield;
133     dvbsub_image_t*         bottomfield;
134     struct dvbsub_object_s* p_next;
135
136 } dvbsub_object_t;
137
138 /* The entry in the palette CLUT */
139 typedef struct
140 {
141     uint8_t                 Y;
142     uint8_t                 Cr;
143     uint8_t                 Cb;
144     uint8_t                 T;
145
146 } dvbsub_color_t;
147
148 /* */
149 typedef struct
150 {
151     uint8_t                 i_id;
152     uint8_t                 i_version_number;
153     dvbsub_color_t          c_2b[0xff];
154     dvbsub_color_t          c_4b[0xff];
155     dvbsub_color_t          c_8b[0xff];
156
157 } dvbsub_clut_t;
158
159 typedef struct
160 {
161     uint8_t                 i_x;
162     uint16_t                i_y;
163     dvbsub_image_t*         p_rle_top;
164     dvbsub_image_t*         p_rle_bot;
165
166 } dvbsub_render_t;
167
168 typedef struct
169 {
170     int i_id;
171
172     mtime_t i_pts;
173
174     dvbsub_clut_t*          p_clut[0xff];
175     dvbsub_page_t*          p_page;
176     dvbsub_object_t*        p_objects;
177     subpicture_t*           p_spu[16];
178     int                     i_subpic_channel;
179
180 } dvbsub_all_t;
181
182 struct subpicture_sys_t
183 {
184     mtime_t         i_pts;
185     void *          p_data;                          /* rle datas are stored */
186     vlc_object_t*   p_input;                            /* Link to the input */
187     vlc_bool_t      b_obsolete;
188 };
189
190 struct decoder_sys_t
191 {
192     vout_thread_t *p_vout;
193
194     bs_t          bs;
195
196     dvbsub_all_t dvbsub;
197 };
198
199
200 // List of different SEGMENT TYPES
201 // According to EN 300-743, table 2
202 #define DVBSUB_ST_PAGE_COMPOSITION      0x10
203 #define DVBSUB_ST_REGION_COMPOSITION    0x11
204 #define DVBSUB_ST_CLUT_DEFINITION       0x12
205 #define DVBSUB_ST_OBJECT_DATA           0x13
206 #define DVBSUB_ST_ENDOFDISPLAY          0x80
207 #define DVBSUB_ST_STUFFING              0xff
208 // List of different OBJECT TYPES
209 // According to EN 300-743, table 6
210 #define DVBSUB_OT_BASIC_BITMAP          0x00
211 #define DVBSUB_OT_BASIC_CHAR            0x01
212 #define DVBSUB_OT_COMPOSITE_STRING      0x02
213 // Pixel DATA TYPES
214 // According to EN 300-743, table 9
215 #define DVBSUB_DT_2BP_CODE_STRING       0x10
216 #define DVBSUB_DT_4BP_CODE_STRING       0x11
217 #define DVBSUB_DT_8BP_CODE_STRING       0x12
218 #define DVBSUB_DT_24_TABLE_DATA         0x20
219 #define DVBSUB_DT_28_TABLE_DATA         0x21
220 #define DVBSUB_DT_48_TABLE_DATA         0x22
221 #define DVBSUB_DT_END_LINE              0xf0
222
223 /*****************************************************************************
224  * Local prototypes
225  *****************************************************************************/
226 static void Decode   ( decoder_t *, block_t ** );
227
228 static vout_thread_t *FindVout( decoder_t * );
229
230 static int  init( dvbsub_all_t *, int );
231 static void decode_segment( decoder_t *, dvbsub_all_t *, bs_t * );
232 static void render( dvbsub_all_t *, vout_thread_t * );
233 static void dvbsub( dvbsub_all_t * );
234
235 /*****************************************************************************
236  * Open: probe the decoder and return score
237  *****************************************************************************
238  * Tries to launch a decoder and return score so that the interface is able
239  * to chose.
240  *****************************************************************************/
241 static int Open( vlc_object_t *p_this )
242 {
243     decoder_t     *p_dec = (decoder_t*) p_this;
244     decoder_sys_t *p_sys;
245
246     if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','v','b','s') )
247     {
248         return VLC_EGENERIC;
249     }
250
251     p_dec->pf_decode_sub = Decode;
252     p_sys = p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
253
254     p_sys->p_vout = NULL;
255
256     init( &p_sys->dvbsub, p_dec->fmt_in.subs.dvb.i_id );
257
258     es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'd','v','b','s' ) );
259
260     return VLC_SUCCESS;
261 }
262
263 /*****************************************************************************
264  * Close:
265  *****************************************************************************/
266 static void Close( vlc_object_t *p_this )
267 {
268     decoder_t     *p_dec = (decoder_t*) p_this;
269     decoder_sys_t *p_sys = p_dec->p_sys;
270
271     if( p_sys->p_vout && p_sys->p_vout->p_subpicture != NULL )
272     {
273         subpicture_t *  p_subpic;
274         int i_subpic;
275         for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
276         {
277             p_subpic = &p_sys->p_vout->p_subpicture[i_subpic];
278             if( p_subpic != NULL &&
279                 ( p_subpic->i_status == RESERVED_SUBPICTURE ||
280                   p_subpic->i_status == READY_SUBPICTURE ) )
281             {
282                 vout_DestroySubPicture( p_sys->p_vout, p_subpic );
283             }
284         }
285     }
286
287     dvbsub( &p_sys->dvbsub );
288
289     free( p_sys );
290 }
291
292 /*****************************************************************************
293  * Decode:
294  *****************************************************************************/
295 static void Decode( decoder_t *p_dec, block_t **pp_block )
296 {
297     decoder_sys_t *p_sys = p_dec->p_sys;
298     block_t       *p_block;
299     vout_thread_t *p_last_vout;
300
301     if( pp_block == NULL || *pp_block == NULL )
302     {
303         return;
304     }
305     p_block = *pp_block;
306     *pp_block = NULL;
307
308     p_sys->dvbsub.i_pts = p_block->i_pts;
309     if( p_sys->dvbsub.i_pts <= 0 )
310     {
311         msg_Warn( p_dec, "non dated subtitle" );
312         block_Release( p_block );
313         return;
314     }
315
316     p_last_vout = p_sys->p_vout;
317     if( ( p_sys->p_vout = FindVout( p_dec ) ) )
318     {
319         int i_data_identifier;
320         int i_subtitle_stream_id;
321         int i_end_data_marker;
322
323         bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
324
325         i_data_identifier = bs_read( &p_sys->bs, 8 );
326         i_subtitle_stream_id = bs_read( &p_sys->bs, 8 );
327
328         for( ;; )
329         {
330             if( bs_show( &p_sys->bs, 8 ) != 0x0f )
331             {
332                 break;
333             }
334             decode_segment( p_dec, &p_sys->dvbsub, &p_sys->bs );
335         }
336         i_end_data_marker = bs_read( &p_sys->bs, 8 );
337
338         if( p_last_vout != p_sys->p_vout )
339         {
340             p_sys->dvbsub.i_subpic_channel =
341                 vout_RegisterOSDChannel( p_sys->p_vout );
342         }
343
344         /* Check if the page is to be displayed */
345         if( p_sys->dvbsub.p_page && p_sys->dvbsub.p_objects )
346         {
347             render( &p_sys->dvbsub, p_sys->p_vout );
348         }
349
350         vlc_object_release( p_sys->p_vout );
351     }
352
353     block_Release( p_block );
354 }
355
356 /* following functions are local */
357 /*****************************************************************************
358  * FindVout: Find a vout or wait for one to be created.
359  *****************************************************************************/
360 static vout_thread_t *FindVout( decoder_t *p_dec )
361 {
362     for( ;; )
363     {
364         vout_thread_t *p_vout;
365
366         if( p_dec->b_die || p_dec->b_error )
367         {
368             return NULL;
369         }
370         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
371         if( p_vout )
372         {
373             return p_vout;
374         }
375         msleep( VOUT_OUTMEM_SLEEP );
376     }
377 }
378
379 static int init( dvbsub_all_t *p_dvbsub, int i_id )
380 {
381     int i;
382
383     memset( p_dvbsub, 0, sizeof( dvbsub_all_t ) );
384
385     p_dvbsub->i_pts     = 0;
386     p_dvbsub->i_id      = i_id;
387     p_dvbsub->p_page    = NULL;
388     p_dvbsub->p_objects = NULL;
389     for( i = 0; i < 255; i++ )
390     {
391         p_dvbsub->p_clut[i] = NULL;
392     }
393     for( i = 0; i < 16; i++ )
394     {
395         p_dvbsub->p_spu[i] = NULL;
396     }
397     return 0;
398 }
399
400 static void free_all( dvbsub_all_t * );
401
402 static void dvbsub( dvbsub_all_t *p_dvbsub )
403 {
404     free_all( p_dvbsub );
405 }
406
407 static void decode_clut( dvbsub_all_t *p_dvbsub, bs_t *s );
408 static void decode_page_composition( dvbsub_all_t *p_dvbsub, bs_t *s);
409 static void decode_region_composition( dvbsub_all_t *p_dvbsub, bs_t *s );
410 static void stop_display( dvbsub_all_t* p_dvbsub );
411 static void decode_object( dvbsub_all_t *p_dvbsub, bs_t *s );
412
413 static void free_page( dvbsub_page_t* p_p );
414
415 static void decode_segment( decoder_t *p_dec, dvbsub_all_t *p_dvbspu, bs_t *s )
416 {
417     int i_type;
418     int i_page_id;
419     int i_size;
420
421     /* sync_byte */
422     bs_skip( s, 8 );
423
424     /* segment type */
425     i_type = bs_read( s, 8 );
426
427     /* page id */
428     i_page_id = bs_read( s, 16 );
429
430     /* segment size */
431     i_size = bs_show( s, 16 );
432
433     if( i_page_id != p_dvbspu->i_id )
434     {
435         bs_skip( s,  8 * ( 2 + i_size ) );
436         return;
437     }
438
439     switch( i_type )
440     {
441     case DVBSUB_ST_CLUT_DEFINITION:
442 #ifdef DEBUG_DVBSUB
443         msg_Dbg( p_dec, "subtitle dvbsub_decode_clut" );
444 #endif
445         decode_clut( p_dvbspu, s );
446         break;
447     case DVBSUB_ST_PAGE_COMPOSITION:
448 #ifdef DEBUG_DVBSUB
449         msg_Dbg( p_dec, "subtitle dvbsub_decode_page_composition" );
450 #endif
451         decode_page_composition( p_dvbspu, s );
452         break;
453     case DVBSUB_ST_REGION_COMPOSITION:
454 #ifdef DEBUG_DVBSUB
455         msg_Dbg( p_dec, "subtitle dvbsub_decode_region_composition" );
456 #endif
457         decode_region_composition( p_dvbspu, s );
458         break;
459     case DVBSUB_ST_OBJECT_DATA:
460 #ifdef DEBUG_DVBSUB
461         msg_Dbg( p_dec, "subtitle dvbsub_decode_object" );
462 #endif
463         decode_object( p_dvbspu, s );
464         break;
465     case DVBSUB_ST_ENDOFDISPLAY:
466 #ifdef DEBUG_DVBSUB
467         msg_Dbg( p_dec, "subtitle dvbsub_stop_display" );
468 #endif
469         stop_display( p_dvbspu );
470         break;
471     case DVBSUB_ST_STUFFING:
472     default:
473         msg_Warn( p_dec, "unsupported segment type: (%04x)", i_type );
474         bs_skip( s,  8 * ( 2 + i_size ) );
475         break;
476     }
477 }
478
479 static void stop_display( dvbsub_all_t *p_dvbsub )
480 {
481     int i;
482
483     for( i = 0; p_dvbsub->p_spu[i] != NULL; i++ )
484     {
485         p_dvbsub->p_spu[i]->i_stop = p_dvbsub->i_pts;
486     }
487 }
488
489 static void decode_clut( dvbsub_all_t *p_dvbsub, bs_t *s )
490 {
491     uint16_t         i_segment_length;
492     uint16_t         i_processed_length;
493     dvbsub_clut_t*   clut;
494     uint8_t          i_clut_id;
495     uint8_t          i_version_number;
496
497     i_segment_length = bs_read( s, 16 );
498     i_clut_id        = bs_read( s, 8 );
499     i_version_number = bs_read( s, 4 );
500
501     // Check that this id doesn't not already exist
502     // with the same version number
503     // And allocate memory if necessary
504     if( p_dvbsub->p_clut[i_clut_id] != NULL)
505     {
506         if( p_dvbsub->p_clut[i_clut_id]->i_version_number == i_version_number )
507         {
508             //TODO skip the right number of bits
509             return;
510         }
511         else
512         {
513             memset( p_dvbsub->p_clut[i_clut_id], 0, sizeof(dvbsub_clut_t) );
514         }
515     }
516     else
517     {
518         p_dvbsub->p_clut[i_clut_id] = malloc( sizeof(dvbsub_clut_t) );
519     }
520     clut = p_dvbsub->p_clut[i_clut_id];
521
522     /* We don't have this version of the CLUT: Parse it */
523     clut->i_version_number = i_version_number;
524     bs_skip( s, 4 ); /* Reserved bits */
525     i_processed_length = 2;
526     while( i_processed_length < i_segment_length )
527     {
528         uint8_t y, cb, cr, t;
529         uint8_t i_id;
530         uint8_t i_type;
531
532         i_id = bs_read( s, 8 );
533         i_type = bs_read( s, 3 );
534
535         bs_skip( s, 4 );
536
537         if( bs_read( s, 1 ) )
538         {
539             y  = bs_read( s, 8 );
540             cr = bs_read( s, 8 );
541             cb = bs_read( s, 8 );
542             t  = bs_read( s, 8 );
543             i_processed_length += 6;
544         }
545         else
546         {
547             y  = bs_read( s, 6 );
548             cr = bs_read( s, 4 );
549             cb = bs_read( s, 4 );
550             t  = bs_read( s, 2 );
551             i_processed_length += 4;
552         }
553
554         /* According to EN 300-743 section 7.2.3 note 1, type should
555          * not have more than 1 bit set to one
556            But, some strams don't respect this note. */
557
558         if( i_type&0x04)
559         {
560             clut->c_2b[i_id].Y = y;
561             clut->c_2b[i_id].Cr = cr;
562             clut->c_2b[i_id].Cb = cb;
563             clut->c_2b[i_id].T = t;
564         }
565         if( i_type&0x02)
566         {
567             clut->c_4b[i_id].Y = y;
568             clut->c_4b[i_id].Cr = cr;
569             clut->c_4b[i_id].Cb = cb;
570             clut->c_4b[i_id].T = t;
571         }
572         if( i_type & 0x01)
573         {
574             clut->c_8b[i_id].Y = y;
575             clut->c_8b[i_id].Cr = cr;
576             clut->c_8b[i_id].Cb = cb;
577             clut->c_8b[i_id].T = t;
578         }
579     }
580 }
581
582 static void decode_page_composition( dvbsub_all_t *p_dvbsub, bs_t *s )
583 {
584     unsigned int i_version_number;
585     unsigned int i_state;
586     unsigned int i_segment_length;
587     uint8_t i_timeout;
588     unsigned int i;
589
590     i_segment_length = bs_read( s, 16 );
591
592     /* A page is composed by one or more region: */
593     i_timeout = bs_read( s, 8 );
594     i_version_number = bs_read( s, 4 );
595     i_state = bs_read( s, 2 );
596
597     /* TODO We assume it is a new page (i_state) */
598     if( p_dvbsub->p_page ) free_page( p_dvbsub->p_page );
599
600     bs_skip( s, 2 ); /* Reserved */
601
602     /* Allocate a new page */
603     p_dvbsub->p_page = malloc( sizeof(dvbsub_page_t) );
604     p_dvbsub->p_page->i_timeout = i_timeout;
605
606     /* Number of regions: */
607     p_dvbsub->p_page->i_regions_number = (i_segment_length-2) / 6;
608
609     /* Special workaround for CAVENA encoders: a page with no regions is sent
610      * instead of a 0x80 packet (End Of Display) */
611     if( p_dvbsub->p_page->i_regions_number == 0 )
612     {
613         stop_display( p_dvbsub );
614     }
615     /* End of workaround */
616
617     p_dvbsub->p_page->regions =
618         malloc( p_dvbsub->p_page->i_regions_number * sizeof(dvbsub_region_t) );
619     for( i = 0; i < p_dvbsub->p_page->i_regions_number; i++ )
620     {
621         p_dvbsub->p_page->regions[i].i_id = bs_read( s, 8 );
622         bs_skip( s, 8 ); /* Reserved */
623         p_dvbsub->p_page->regions[i].i_x = bs_read( s, 16 );
624         p_dvbsub->p_page->regions[i].i_y = bs_read( s, 16 );
625         p_dvbsub->p_page->regions[i].p_object = NULL;
626     }
627 }
628
629
630 static void decode_region_composition( dvbsub_all_t *p_dvbsub, bs_t *s )
631 {
632     dvbsub_region_t* p_region = NULL;
633     unsigned int i_segment_length;
634     unsigned int i_processed_length;
635     unsigned int i_region_id;
636     unsigned int i;
637
638     i_segment_length = bs_read( s, 16 );
639
640     /* Get region id: */
641     i_region_id = bs_read( s, 8 );
642     for( i = 0; i < p_dvbsub->p_page->i_regions_number; i++ )
643     {
644         if( p_dvbsub->p_page->regions[i].i_id ==  i_region_id )
645         {
646             p_region = &(p_dvbsub->p_page->regions[i]);
647         }
648     }
649
650     if( p_region == NULL )
651     {
652         /* TODO
653          * The region has never been declared before
654          * Internal error */
655         fprintf( stderr, "Decoding of undeclared region N/A\n" );
656         return;
657     }
658
659     /* Skip version number and fill flag */
660     if( bs_show( s, 4 ) == p_region->i_version_number )
661     {
662         fprintf( stderr, "Skipping already known region N/A\n" );
663         /* TODO Skip the right number of bits */
664     }
665
666     /* Region attributes */
667     p_region->i_version_number = bs_read( s, 4 );
668     p_region->b_fill           = bs_read( s, 1 );
669     bs_skip( s, 3 ); /* Reserved */
670     p_region->i_width          = bs_read( s, 16 );
671     p_region->i_height         = bs_read( s, 16 );
672     p_region->i_level_comp     = bs_read( s, 3 );
673     p_region->i_depth          = bs_read( s, 3 );
674     bs_skip( s, 2 ); /* Reserved */
675     p_region->i_clut           = bs_read( s, 8 );
676     p_region->i_8bp_code       = bs_read( s, 8 );
677     p_region->i_4bp_code       = bs_read( s, 4 );
678     p_region->i_2bp_code       = bs_read( s, 2 );
679     bs_skip( s, 2 ); /* Reserved */
680
681     /* List of objects in the region: */
682     /* We already skipped 10 bytes */
683
684     i_processed_length = 10;
685     while( i_processed_length < i_segment_length )
686     {
687         /* We create a new object */
688         dvbsub_objectdef_t *p_obj = malloc( sizeof(dvbsub_objectdef_t) );
689
690         /* We parse object properties */
691         p_obj->p_next       = NULL;
692         p_obj->i_id         = bs_read( s, 16 );
693         p_obj->i_type       = bs_read( s, 2 );
694         p_obj->i_provider   = bs_read( s, 2 );
695         p_obj->i_xoffset    = bs_read( s, 12 );
696         bs_skip( s, 4 ); /* Reserved */
697         p_obj->i_yoffset    = bs_read( s, 12 );
698
699         i_processed_length += 6;
700
701         if( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ||
702             p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING )
703         {
704             p_obj->i_fg_pc =  bs_read( s, 8 );
705             p_obj->i_bg_pc =  bs_read( s, 8 );
706             i_processed_length += 2;
707         }
708
709         /* we append it */
710         if( p_region->p_object )
711         {
712             dvbsub_objectdef_t *p_o;
713             for( p_o = p_region->p_object; ; p_o = p_o->p_next )
714             {
715                 if( p_o->p_next == NULL )
716                 {
717                     break;
718                 }
719             }
720             p_o->p_next = p_obj;
721         }
722         else
723         {
724             p_region->p_object = p_obj;
725         }
726     }
727 }
728
729 static dvbsub_image_t* dvbsub_parse_pdata( dvbsub_all_t *p_dvbsub, bs_t *s,
730                                            uint16_t length );
731 static uint16_t dvbsub_count0x11( bs_t *s, uint16_t* p,
732                                   dvbsub_image_t* p_image);
733
734 static void decode_object( dvbsub_all_t *p_dvbsub, bs_t *s )
735 {
736     dvbsub_object_t *p_obj;
737     uint16_t        i_segment_length;
738
739     /* Memory Allocation */
740     p_obj = malloc( sizeof(dvbsub_object_t) );
741     p_obj->p_next = NULL;
742
743     i_segment_length = bs_read( s, 16 );
744
745     /* TODO Check we don't already have this object / this version */
746     p_obj->i_id              =  bs_read( s, 16 );
747     p_obj->i_version_number  = bs_read( s, 4 );
748     p_obj->i_coding_method   = bs_read( s, 2 );
749     p_obj->b_non_modify_color= bs_read( s, 1 );
750     bs_skip( s, 1 ); /* Reserved */
751
752     if( p_obj->i_coding_method == 0x00 )
753     {
754         uint16_t i_topfield_length;
755         uint16_t i_bottomfield_length;
756
757         i_topfield_length    = bs_read( s, 16 );
758         i_bottomfield_length = bs_read( s, 16 );
759
760         p_obj->topfield =
761             dvbsub_parse_pdata( p_dvbsub, s, i_topfield_length );
762         p_obj->bottomfield =
763             dvbsub_parse_pdata( p_dvbsub, s, i_bottomfield_length );
764     }
765     else
766     {
767         bs_skip( s, (i_segment_length - 3 ) * 8 );
768         /*TODO: DVB subtitling as characters */
769     }
770
771     /* Add this object to the list of the page */
772     p_obj->p_next = p_dvbsub->p_objects;
773     p_dvbsub->p_objects = p_obj;
774 }
775
776 static dvbsub_image_t* dvbsub_parse_pdata( dvbsub_all_t *p_dvbsub, bs_t *s,
777                                            uint16_t length )
778 {
779     dvbsub_image_t* p_image;
780     uint16_t i_processed_length = 0;
781     uint16_t i_lines = 0;
782     uint16_t i_cols_last = 0;
783
784     p_image = malloc( sizeof(dvbsub_image_t) );
785     p_image->p_last = NULL;
786
787     memset( p_image->i_cols, 0, 576 * sizeof(uint16_t) );
788
789     /* Let's parse it a first time to determine the size of the buffer */
790     while( i_processed_length < length)
791     {
792         switch( bs_read( s, 8 ) )
793         {
794             case 0x10:
795                 fprintf(stderr, "0x10 N/A\n");
796                 break;
797             case 0x11:
798                 i_processed_length +=
799                     1 + dvbsub_count0x11( s, &(p_image->i_cols[i_lines]),
800                                           p_image );
801                 break;
802             case 0x12:
803                 fprintf(stderr, "0x12 N/A\n");
804                 break;
805             case 0x20:
806                 fprintf(stderr, "0x20 N/A\n");
807                 break;
808             case 0x21:
809                 fprintf(stderr, "0x21 N/A\n");
810                 break;
811             case 0x22:
812                 fprintf(stderr, "0x22 N/A\n");
813                 break;
814             case 0xf0:
815                 i_processed_length++;
816                 i_lines++;
817                 break;
818         }
819     }
820
821     p_image->i_rows =  i_lines;
822     p_image->i_cols[i_lines] = i_cols_last;
823
824     /* Check word-aligned bits */
825     if( bs_show( s, 8 ) == 0x00 )
826     {
827         bs_skip( s, 8 );
828     }
829
830     return p_image;
831 }
832
833 static void add_rle_code( dvbsub_image_t *p, uint16_t num, uint8_t color )
834 {
835     if(p->p_last != NULL)
836     {
837         p->p_last->p_next = malloc( sizeof(dvbsub_rle_t) );
838         p->p_last = p->p_last->p_next;
839     }
840     else
841     {
842         p->p_codes =  malloc( sizeof(dvbsub_rle_t) );
843         p->p_last = p->p_codes;
844     }
845     p->p_last->i_num = num;
846     p->p_last->i_color_code = color;
847     p->p_last->p_next = NULL;
848 }
849
850 static uint16_t dvbsub_count0x11( bs_t *s, uint16_t* p,
851                                   dvbsub_image_t* p_image )
852 {
853     uint16_t i_processed=0;
854     vlc_bool_t b_stop=0;
855     uint16_t i_count = 0;
856     uint8_t i_color =0;
857
858     while (!b_stop)
859     {
860         if( (i_color = bs_read( s, 4 )) != 0x00 )
861         {
862             (*p)++;
863             i_processed+=4;
864
865             /* 1 pixel of color code '0000' */
866             add_rle_code( p_image, 1, i_color );
867         }
868         else
869         {
870             if( bs_read( s, 1 ) == 0x00 )           // Switch1
871             {
872                 if( bs_show( s, 3 ) != 0x00 )
873                 {
874                     i_count = 2 + bs_read( s, 3 );
875                     (*p) += i_count ;
876                     add_rle_code( p_image, i_count, 0x00 );
877                 }
878                 else
879                 {
880                     bs_skip( s, 3 );
881                     b_stop=1;
882                 }
883                 i_processed += 8;
884             }
885             else
886             {
887                 if( bs_read( s, 1 ) == 0x00)        //Switch2
888                 {
889                     i_count =  4 + bs_read( s, 2 );
890                     i_color = bs_read( s, 4 );
891                     (*p) += i_count;
892                     i_processed += 12;
893                     add_rle_code( p_image, i_count, i_color );
894                 }
895                 else
896                 {
897                     switch ( bs_read( s, 2 ) )     //Switch3
898                     {
899                         case 0x0:
900                             (*p)++;
901                             i_processed += 8;
902                             add_rle_code( p_image, 1, 0x00 );
903                             break;
904                         case 0x1:
905                             (*p)+=2;
906                             i_processed += 8;
907                             add_rle_code( p_image, 2, 0x00 );
908                             break;
909                         case 0x2:
910                              i_count = 9 + bs_read( s, 4 );
911                              i_color = bs_read( s, 4 );
912                              (*p)+= i_count;
913                              i_processed += 16;
914                              add_rle_code( p_image, i_count, i_color );
915                              break;
916                         case 0x3:
917                              i_count= 25 + bs_read( s, 8 );
918                              i_color = bs_read( s, 4 );
919                              (*p)+= i_count;
920                              i_processed += 20;
921                              add_rle_code( p_image, i_count, i_color );
922                              break;
923                     }
924                 }
925             }
926         }
927     }
928
929     bs_align( s );
930
931     return ( i_processed + 7 ) / 8 ;
932 }
933
934 static void free_image (dvbsub_image_t* p_i)
935 {
936     dvbsub_rle_t* p1;
937     dvbsub_rle_t* p2=NULL;
938
939     for( p1 = p_i->p_codes; p1 != NULL; p1=p2)
940     {
941         p2=p1->p_next;
942         free(p1);
943         p1=NULL;
944     }
945
946     free(p_i);
947 }
948
949 static void free_object (dvbsub_object_t* p_o)
950 {
951     free(p_o);
952 }
953
954 static void free_objectdefs ( dvbsub_objectdef_t* p_o)
955 {
956     dvbsub_objectdef_t* p1;
957     dvbsub_objectdef_t* p2=NULL;
958
959     for( p1 = p_o; p1 != NULL; p1=p2)
960     {
961         p2=p1->p_next;
962         free(p1);
963         p1=NULL;
964     }
965 }
966
967 static void free_regions (dvbsub_region_t* p_r, uint8_t nb)
968 {
969     unsigned int i;
970
971     for (i = 0; i<nb; i++) free_objectdefs ( p_r[i].p_object );
972     free (p_r);
973     p_r = NULL;
974 }
975
976 static void free_objects (dvbsub_object_t* p_o)
977 {
978     dvbsub_object_t* p1;
979     dvbsub_object_t* p2=NULL;
980
981     for( p1 = p_o; p1 != NULL; p1=p2)
982     {
983         p2=p1->p_next;
984         free_image (p1->topfield);
985         free_image (p1->bottomfield);
986         free_object(p1);
987     }
988 }
989
990 static void free_clut ( dvbsub_clut_t* p_c )
991 {
992     free(p_c);
993 }
994
995 static void free_page (dvbsub_page_t* p_p)
996 {
997     free_regions (p_p->regions, p_p->i_regions_number);
998     free(p_p);
999     p_p = NULL;
1000 }
1001
1002 static void free_spu( subpicture_t *p_spu )
1003 {
1004     if ( p_spu->p_sys )
1005     {
1006         free_image(((dvbsub_render_t *)p_spu->p_sys->p_data)->p_rle_top);
1007         free_image(((dvbsub_render_t *)p_spu->p_sys->p_data)->p_rle_bot);
1008         free(p_spu->p_sys->p_data);
1009         free( p_spu->p_sys );
1010         p_spu->p_sys = NULL;
1011     }
1012 }
1013
1014 static void free_all ( dvbsub_all_t* p_a )
1015 {
1016     unsigned int i;
1017
1018     for(i=0; i<0xff; i++) if (p_a->p_clut[i]) free_clut ( p_a->p_clut[i] );
1019     for(i=0; i<16; i++) if (p_a->p_spu[i]) free_spu ( p_a->p_spu[i] );
1020     if(p_a->p_page) free_page( p_a->p_page );
1021     free_objects (p_a->p_objects);
1022 }
1023
1024 static void RenderYUY2( vout_thread_t *p_vout, picture_t *p_pic,
1025                         const subpicture_t *p_spu )
1026 {
1027     /* Common variables */
1028     uint8_t  *p_desty;
1029     uint16_t i,j;
1030     uint16_t i_cnt;
1031     uint16_t x, y;
1032     dvbsub_rle_t* p_c;
1033     dvbsub_render_t* p_r = ((dvbsub_render_t *)p_spu->p_sys->p_data);
1034     dvbsub_image_t* p_im = p_r->p_rle_top;
1035     i=0;
1036     j=0;
1037     p_desty = p_pic->Y_PIXELS;
1038     //let's render the 1st frame
1039     for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1040     {
1041         //if( p_c->y != 0  && p_c->t < 0x20)
1042         if( p_c->y != 0  && p_c->t < 0x20)
1043         {
1044             x = j+ p_r->i_x;
1045             y = 2*i+p_r->i_y;
1046             //memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1047             // In YUY2 we have to set pixel per pixel
1048             for( i_cnt = 0; i_cnt < p_c->i_num; i_cnt+=2 )
1049             {
1050                 memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt, p_c->y, 1);
1051               //memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+1, p_c->cr, 1);
1052               //memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+2, p_c->y, 1);
1053               //memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+3, p_c->cb, 1);
1054             }
1055         }
1056         j += p_c->i_num;
1057         if(j >= p_im->i_cols[i])
1058         {
1059             i++; j=0;
1060         }
1061         if( i>= p_im->i_rows) break;
1062     }
1063     //idem for the second frame
1064     p_im = p_r->p_rle_bot; i=0; j=0;
1065     for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1066     {
1067         if( p_c->y != 0 && p_c->t < 0x20)
1068         {
1069             x = j+ p_r->i_x;
1070             y = 2*i+1+p_r->i_y;
1071             //memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1072             // In YUY2 we have to set pixel per pixel
1073             for( i_cnt = 0; i_cnt < p_c->i_num; i_cnt+=2 )
1074             {
1075                 memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt, p_c->y, 1);
1076               //memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+1, p_c->cr, 1);
1077               //memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+2, p_c->y, 1);
1078               //memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+3, p_c->cb, 1);
1079            }
1080         }
1081         j += p_c->i_num;
1082         if(j >= p_im->i_cols[i])
1083         {
1084             i++; j=0;
1085         }
1086         if( i>= p_im->i_rows) break;
1087     }
1088 }
1089
1090 static void RenderI42x( vout_thread_t *p_vout, picture_t *p_pic,
1091                         const subpicture_t *p_spu )
1092 {
1093     /* Common variables */
1094     uint8_t  *p_desty;
1095     uint8_t  *p_destu;
1096     uint8_t  *p_destv;
1097     uint16_t i,j;
1098     uint16_t x, y;
1099     dvbsub_rle_t* p_c;
1100     dvbsub_render_t* p_r = ((dvbsub_render_t *)p_spu->p_sys->p_data);
1101     dvbsub_image_t* p_im = p_r->p_rle_top;
1102     i=0;
1103     j=0;
1104     p_desty = p_pic->Y_PIXELS;
1105     p_destu = p_pic->U_PIXELS;
1106     p_destv = p_pic->V_PIXELS;
1107     //let's render the 1st frame
1108     for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1109     {
1110         if( p_c->y != 0 )
1111         {
1112             x = j+ p_r->i_x;
1113             y = 2*i+p_r->i_y;
1114             //memset(p_dest+ y*p_pic->U_PITCH*2 + x, p_c->cr, p_c->i_num);
1115             //memset(p_desty+ (y)*p_pic->Y_PITCH + x, p_c->cr, p_c->i_num);
1116             //memset(p_dest+ y*p_pic->V_PITCH*2 + x, p_c->cb, p_c->i_num);
1117             //memset(p_destu+ (y)*p_pic->Y_PITCH + x, p_c->cb, p_c->i_num);
1118             memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1119             //memset(p_desty+ 2*y*p_pic->U_PITCH + x, p_c->cr, p_c->i_num);
1120             //memset(p_desty+ 2*y*p_pic->V_PITCH + x, p_c->cb, p_c->i_num);
1121         }
1122         j += p_c->i_num;
1123         if(j >= p_im->i_cols[i])
1124         {
1125             i++; j=0;
1126         }
1127         if( i>= p_im->i_rows) break;
1128     }
1129     //idem for the second frame
1130     p_im = p_r->p_rle_bot; i=0; j=0;
1131     for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1132     {
1133         if( p_c->y != 0 && p_c->t < 0x20)
1134         {
1135             x = j+ p_r->i_x;
1136             y = 2*i+1+p_r->i_y;
1137             //memset(p_desty+ y*p_pic->U_PITCH*2 + x, p_c->cr, p_c->i_num);
1138             //memset(p_desty+ y*p_pic->V_PITCH*2 + x, p_c->cb, p_c->i_num);
1139             memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1140             //memset(p_desty+ 2*y*p_pic->U_PITCH + x, p_c->cr, p_c->i_num);
1141             //memset(p_desty+ 2*y*p_pic->V_PITCH + x, p_c->cb, p_c->i_num);
1142         }
1143         j += p_c->i_num;
1144         if(j >= p_im->i_cols[i])
1145         {
1146             i++; j=0;
1147         }
1148         if( i>= p_im->i_rows) break;
1149     }
1150 }
1151
1152 static void RenderDVBSUB( vout_thread_t *p_vout, picture_t *p_pic,
1153                           const subpicture_t *p_spu )
1154 {
1155     /* If we have changed the language on the fly */
1156
1157     if( p_spu->p_sys == NULL || p_spu->p_sys->b_obsolete )
1158     {
1159         return;
1160     }
1161
1162     switch( p_vout->output.i_chroma )
1163     {
1164         /* I420 target, no scaling */
1165         case VLC_FOURCC('I','4','2','2'):
1166         case VLC_FOURCC('I','4','2','0'):
1167         case VLC_FOURCC('I','Y','U','V'):
1168         case VLC_FOURCC('Y','V','1','2'):
1169             /* As long as we just use Y info, I422 and YV12 are just equivalent
1170              * to I420. Remember to change it the day we'll take into account
1171              * U and V info. */
1172             RenderI42x( p_vout, p_pic, p_spu );
1173             break;
1174
1175         /* RV16 target, scaling */
1176         case VLC_FOURCC('R','V','1','6'):
1177             msg_Err(p_vout, "unimplemented chroma: RV16");
1178             /* RenderRV16( p_vout, p_pic, p_spu ); */
1179             break;
1180
1181         /* RV32 target, scaling */
1182         case VLC_FOURCC('R','V','2','4'):
1183         case VLC_FOURCC('R','V','3','2'):
1184             msg_Err(p_vout, "unimplemented chroma: RV32");
1185             /* RenderRV32( p_vout, p_pic, p_spu ); */
1186             break;
1187
1188         /* NVidia overlay, no scaling */
1189         case VLC_FOURCC('Y','U','Y','2'):
1190             RenderYUY2( p_vout, p_pic, p_spu );
1191             break;
1192
1193         default:
1194             msg_Err( p_vout, "unknown chroma, can't render SPU" );
1195             break;
1196     }
1197 }
1198
1199 static void dvbsub_Destroy( subpicture_t *p_spu )
1200 {
1201     free_spu( p_spu );
1202 }
1203
1204 static void render( dvbsub_all_t *dvbsub, vout_thread_t *p_vout )
1205 {
1206     dvbsub_region_t*     p_region;
1207     dvbsub_objectdef_t*  p_objectdef;
1208     dvbsub_object_t*     p_o;
1209     dvbsub_object_t*     p_object;
1210     dvbsub_object_t*     p_object_old;
1211     dvbsub_render_t*     p_render;
1212     dvbsub_rle_t*        p_c;
1213     uint8_t i , j = 0;
1214
1215     /* loop on regions */
1216     for( i = 0; i < dvbsub->p_page->i_regions_number; i++ )
1217     {
1218         p_region = &(dvbsub->p_page->regions[i]);
1219
1220         /* loop on objects */
1221         for( p_objectdef = p_region->p_object; p_objectdef != NULL;
1222              p_objectdef = p_objectdef->p_next )
1223         {
1224             /* Look for the right object */
1225             p_object = dvbsub->p_objects;
1226             while( !p_object && p_object->i_id != p_objectdef->i_id )
1227             {
1228                 p_object = p_object->p_next;
1229             }
1230
1231             if( !p_object )
1232             {
1233                 msg_Err( p_vout, "internal decoder error");
1234                 return;
1235             }
1236
1237             /* Allocate the render structure */
1238             p_render = malloc( sizeof(dvbsub_render_t) );
1239             p_render->i_x = p_region->i_x + p_objectdef->i_xoffset;
1240             p_render->i_y = p_region->i_y + p_objectdef->i_yoffset;
1241             p_render->p_rle_top = p_object->topfield;
1242             p_render->p_rle_bot = p_object->bottomfield;
1243
1244             // if we did not recieved the CLUT yet
1245             if( !dvbsub->p_clut[p_region->i_clut] ) return;
1246
1247             /* Compute the color datas according to the appropriate CLUT */
1248             for( p_c = p_render->p_rle_top->p_codes;
1249                  p_c->p_next != NULL; p_c = p_c->p_next )
1250             {
1251                 //TODO We assume here we are working in 4bp
1252                 p_c->y = dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Y;
1253                 p_c->cr = dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Cr;
1254                 p_c->cb = dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Cb;
1255                 p_c->t = dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].T;
1256             }
1257             for( p_c = p_render->p_rle_bot->p_codes; p_c->p_next != NULL;
1258                  p_c = p_c->p_next )
1259             {
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;
1265             }
1266
1267
1268             /* Allocate the subpicture internal data. */
1269             dvbsub->p_spu[j] =
1270                 vout_CreateSubPicture( p_vout, dvbsub->i_subpic_channel,
1271                                        MEMORY_SUBPICTURE );
1272             if( dvbsub->p_spu[j] == NULL )
1273             {
1274                 msg_Err(p_vout, "Unable to allocate memory, skipping");
1275                 return;
1276             }
1277             /* Set the pf_render callback */
1278             dvbsub->p_spu[j]->pf_render = RenderDVBSUB;
1279             dvbsub->p_spu[j]->p_sys = malloc( sizeof(subpicture_sys_t) );
1280             dvbsub->p_spu[j]->p_sys->p_data = p_render;
1281             dvbsub->p_spu[j]->p_sys->b_obsolete = 0;
1282             dvbsub->p_spu[j]->pf_destroy = dvbsub_Destroy;
1283             dvbsub->p_spu[j]->i_start = dvbsub->i_pts;
1284             dvbsub->p_spu[j]->i_stop = dvbsub->p_spu[j]->i_start +
1285                 dvbsub->p_page->i_timeout * 1000000;
1286             dvbsub->p_spu[j]->b_ephemer = VLC_FALSE;
1287
1288             // At this stage, we have all we need in p_render
1289             // We need to free the object
1290             //Remove this object from the list
1291             p_object_old = p_object;
1292             if( p_object == dvbsub->p_objects )
1293             {
1294                 dvbsub->p_objects = p_object->p_next;
1295             }
1296             else
1297             {
1298                for( p_o = dvbsub->p_objects; p_o->p_next != p_object;
1299                     p_o = p_o->p_next );
1300                p_o->p_next = p_object->p_next;
1301             }
1302             free_object(p_object_old);
1303
1304             vout_DisplaySubPicture( p_vout, dvbsub->p_spu[j] );
1305
1306             j++;
1307         }
1308     }
1309 }