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