]> git.sesse.net Git - vlc/blob - modules/codec/dvbsub.c
* all: ported/cleaned up/renabled dvb subtitle decoder. But it is untested.
[vlc] / modules / codec / dvbsub.c
1 /*****************************************************************************
2  * dvbsub.c : DVB subtitles decoder thread
3  *****************************************************************************
4  * Copyright (C) 2003 ANEVIA
5  * Copyright (C) 2003 VideoLAN
6  * $Id: dvbsub.c,v 1.6 2003/11/24 02:35:50 fenrir Exp $
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     add_category_hint( N_("subtitles"), NULL, VLC_TRUE );
42     set_description( _("subtitles decoder") );
43     set_capability( "decoder", 50 );
44     set_callbacks( Open, Close );
45 vlc_module_end();
46
47
48 // Wow, that's ugly but very usefull for a memory leak track
49 // so I just keep it
50 #if 0
51 static long long unsigned int trox_malloc_nb = 0;
52 static long long unsigned int trox_free_nb = 0;
53
54 static void* trox_malloc (size_t size)
55 { ++trox_malloc_nb; return malloc (size); }
56
57 static void trox_free (void* ptr)
58 { ++trox_free_nb; free(ptr); return; }
59
60 static void trox_call ()
61 {
62   fprintf(stderr, "dvbbsub -- Memory usage:  %llu mallocs %llu frees (%llu)\n",
63                   trox_malloc_nb,
64                   trox_free_nb,
65                   trox_malloc_nb - trox_free_nb);
66   return;
67 }
68 #else
69 # define trox_malloc malloc
70 # define trox_free free
71 # define trox_call()
72 #endif
73 /****************************************************************************
74  * Local structures
75  ****************************************************************************
76  * Those structures refer closely to the ETSI 300 743 Object model
77  ****************************************************************************/
78
79 /* Storage of a RLE entry */
80 typedef struct dvbsub_rle_s
81 {
82     uint16_t                 i_num;
83     uint8_t                 i_color_code;
84     uint8_t                 y;
85     uint8_t                 cr;
86     uint8_t                 cb;
87     uint8_t                 t;
88     struct dvbsub_rle_s*    p_next;
89 } dvbsub_rle_t;
90
91 /* A subpicture image is a list of codes
92  * We need to store the length of each line since nothing specify in
93  * the standard that all lines shoudl have the same length
94  * WARNING: We assume here that a spu is less than 576 lines high */
95 typedef struct
96 {
97     uint16_t                        i_rows;
98     uint16_t                        i_cols[576];
99     dvbsub_rle_t*                   p_last;
100     dvbsub_rle_t*                   p_codes;
101 } dvbsub_image_t;
102
103 /* The object definition gives the position of the object in a region */
104 typedef struct dvbsub_objectdef_s
105 {
106     uint16_t                    i_id;
107     uint8_t                     i_type;
108     uint8_t                     i_provider;
109     uint16_t                    i_xoffset;
110     uint16_t                    i_yoffset;
111     uint8_t                     i_fg_pc;
112     uint8_t                     i_bg_pc;
113     struct dvbsub_objectdef_s*  p_next;
114 } dvbsub_objectdef_t;
115
116 /* The Region is an aera on the image
117  * with a list of the object definitions associated
118  * and a CLUT */
119 typedef struct dvbsub_region_s
120 {
121     uint8_t                 i_id;
122     uint8_t                 i_version_number;
123     vlc_bool_t              b_fill;
124     uint16_t                i_x;
125     uint16_t                i_y;
126     uint16_t                i_width;
127     uint16_t                i_height;
128     uint8_t                 i_level_comp;
129     uint8_t                 i_depth;
130     uint8_t                 i_clut;
131     uint8_t                 i_8bp_code;
132     uint8_t                 i_4bp_code;
133     uint8_t                 i_2bp_code;
134     dvbsub_objectdef_t*    p_object;
135 } dvbsub_region_t;
136
137 /* The page defines the list of regions */
138 typedef struct
139 {
140     uint16_t              i_id;
141     uint8_t               i_timeout;
142     uint8_t               i_state;
143     uint8_t               i_version_number;
144     uint8_t               i_regions_number;
145     dvbsub_region_t*      regions;
146 } dvbsub_page_t;
147
148 /* An object is constituted of 2 images (for interleaving) */
149 typedef struct dvbsub_object_s
150 {
151     uint16_t                i_id;
152     uint8_t                 i_version_number;
153     uint8_t                 i_coding_method;
154     vlc_bool_t              b_non_modify_color;
155     dvbsub_image_t*         topfield;
156     dvbsub_image_t*         bottomfield;
157     struct dvbsub_object_s* p_next;
158 } dvbsub_object_t;
159
160 /* The entry in the palette CLUT */
161 typedef struct
162 {
163     uint8_t                 Y;
164     uint8_t                 Cr;
165     uint8_t                 Cb;
166     uint8_t                 T;
167 } dvbsub_color_t;
168
169 /* */
170 typedef struct
171 {
172     uint8_t                 i_id;
173     uint8_t                 i_version_number;
174     dvbsub_color_t          c_2b[0xff];
175     dvbsub_color_t          c_4b[0xff];
176     dvbsub_color_t          c_8b[0xff];
177 } dvbsub_clut_t;
178
179 typedef struct
180 {
181     uint8_t                 i_x;
182     uint16_t                i_y;
183     dvbsub_image_t*         p_rle_top;
184     dvbsub_image_t*         p_rle_bot;
185 } dvbsub_render_t;
186
187 typedef struct
188 {
189     int i_id;
190
191     mtime_t i_pts;
192
193     dvbsub_clut_t*          p_clut[0xff];
194     dvbsub_page_t*          p_page;
195     dvbsub_object_t*        p_objects;
196     subpicture_t*           p_spu[16];
197 } dvbsub_all_t;
198
199 struct subpicture_sys_t
200 {
201     mtime_t         i_pts;
202     void *          p_data;                          /* rle datas are stored */
203     vlc_object_t*   p_input;                            /* Link to the input */
204     vlc_bool_t      b_obsolete;
205 };
206
207 struct decoder_sys_t
208 {
209     vout_thread_t *p_vout;
210
211     bs_t          bs;
212
213     dvbsub_all_t dvbsub;
214 };
215
216
217 // List of different SEGMENT TYPES
218 // According to EN 300-743, table 2
219 #define DVBSUB_ST_PAGE_COMPOSITION      0x10
220 #define DVBSUB_ST_REGION_COMPOSITION    0x11
221 #define DVBSUB_ST_CLUT_DEFINITION       0x12
222 #define DVBSUB_ST_OBJECT_DATA           0x13
223 #define DVBSUB_ST_ENDOFDISPLAY          0x80
224 #define DVBSUB_ST_STUFFING              0xff
225 // List of different OBJECT TYPES
226 // According to EN 300-743, table 6
227 #define DVBSUB_OT_BASIC_BITMAP          0x00
228 #define DVBSUB_OT_BASIC_CHAR            0x01
229 #define DVBSUB_OT_COMPOSITE_STRING      0x02
230 // Pixel DATA TYPES
231 // According to EN 300-743, table 9
232 #define DVBSUB_DT_2BP_CODE_STRING       0x10
233 #define DVBSUB_DT_4BP_CODE_STRING       0x11
234 #define DVBSUB_DT_8BP_CODE_STRING       0x12
235 #define DVBSUB_DT_24_TABLE_DATA         0x20
236 #define DVBSUB_DT_28_TABLE_DATA         0x21
237 #define DVBSUB_DT_48_TABLE_DATA         0x22
238 #define DVBSUB_DT_END_LINE              0xf0
239
240 /*****************************************************************************
241  * Local prototypes
242  *****************************************************************************/
243 static void Decode   ( decoder_t *, block_t ** );
244
245 static vout_thread_t *FindVout( decoder_t * );
246
247 static int  dvbsub_init( dvbsub_all_t *, int  );
248 static void dvbsub_decode_segment( dvbsub_all_t *p_dvbsub, bs_t *s );
249 static void dvbsub_render( dvbsub_all_t *, vout_thread_t * );
250 static void dvbsub_clean( dvbsub_all_t * );
251
252
253 /*****************************************************************************
254  * Open: probe the decoder and return score
255  *****************************************************************************
256  * Tries to launch a decoder and return score so that the interface is able
257  * to chose.
258  *****************************************************************************/
259 static int Open( vlc_object_t *p_this )
260 {
261     decoder_t     *p_dec = (decoder_t*) p_this;
262     decoder_sys_t *p_sys;
263
264     if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','v','b','s') )
265     {
266         return VLC_EGENERIC;
267     }
268
269     p_dec->pf_decode_sub = Decode;
270     p_sys = p_dec->p_sys = malloc( sizeof( decoder_sys_t ) );
271
272     p_sys->p_vout = NULL;
273
274     dvbsub_init( &p_sys->dvbsub, p_dec->fmt_in.subs.dvb.i_id );
275
276     es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'd','v','b','s' ) );
277
278     return VLC_SUCCESS;
279 }
280
281 /*****************************************************************************
282  * Close:
283  *****************************************************************************/
284 static void Close( vlc_object_t *p_this )
285 {
286     decoder_t     *p_dec = (decoder_t*) p_this;
287     decoder_sys_t *p_sys = p_dec->p_sys;
288
289     if( p_sys->p_vout && p_sys->p_vout->p_subpicture != NULL )
290     {
291         subpicture_t *  p_subpic;
292         int i_subpic;
293         for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
294         {
295             p_subpic = &p_sys->p_vout->p_subpicture[i_subpic];
296             if( p_subpic != NULL &&
297               ( ( p_subpic->i_status == RESERVED_SUBPICTURE ) ||
298                 ( p_subpic->i_status == READY_SUBPICTURE ) ) )
299             {
300                 vout_DestroySubPicture( p_sys->p_vout, p_subpic );
301             }
302         }
303     }
304
305     dvbsub_clean( &p_sys->dvbsub );
306
307     free( p_sys );
308 }
309
310 /*****************************************************************************
311  * Decode:
312  *****************************************************************************/
313 static void Decode( decoder_t *p_dec, block_t **pp_block )
314 {
315     decoder_sys_t *p_sys = p_dec->p_sys;
316     block_t       *p_block;
317
318     if( pp_block == NULL || *pp_block == NULL )
319     {
320         return;
321     }
322     p_block = *pp_block;
323     *pp_block = NULL;
324
325     p_sys->dvbsub.i_pts = p_block->i_pts;
326     if( p_sys->dvbsub.i_pts <= 0 )
327     {
328         msg_Warn( p_dec, "non dated subtitle" );
329         block_Release( p_block );
330         return;
331     }
332
333     if( ( p_sys->p_vout = FindVout( p_dec ) ) )
334     {
335         int i_data_identifier;
336         int i_subtitle_stream_id;
337         int i_end_data_marker;
338
339         bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
340
341         i_data_identifier = bs_read( &p_sys->bs, 8 );
342         i_subtitle_stream_id = bs_read( &p_sys->bs, 8 );
343
344         for( ;; )
345         {
346             if( bs_show( &p_sys->bs, 8 ) != 0x0f )
347             {
348                 break;
349             }
350             dvbsub_decode_segment( &p_sys->dvbsub, &p_sys->bs );
351         }
352         i_end_data_marker = bs_read( &p_sys->bs, 8 );
353
354         /* Check if the page is to be displayed */
355         if( p_sys->dvbsub.p_page && p_sys->dvbsub.p_objects )
356         {
357             dvbsub_render( &p_sys->dvbsub, p_sys->p_vout );
358         }
359
360         vlc_object_release( p_sys->p_vout );
361     }
362
363     block_Release( p_block );
364 }
365
366 /* following functions are local */
367 /*****************************************************************************
368  * FindVout: Find a vout or wait for one to be created.
369  *****************************************************************************/
370 static vout_thread_t *FindVout( decoder_t *p_dec )
371 {
372     for( ;; )
373     {
374         vout_thread_t *p_vout;
375
376         if( p_dec->b_die || p_dec->b_error )
377         {
378             return NULL;
379         }
380         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
381         if( p_vout )
382         {
383             return p_vout;
384         }
385         msleep( VOUT_OUTMEM_SLEEP );
386     }
387 }
388
389
390 static int dvbsub_init( dvbsub_all_t *p_dvbsub, int i_id )
391 {
392     int i;
393
394     memset( p_dvbsub, 0, sizeof( dvbsub_all_t ) );
395
396     p_dvbsub->i_pts     = 0;
397     p_dvbsub->i_id      = i_id;
398     p_dvbsub->p_page     = NULL;
399     p_dvbsub->p_objects  = NULL;
400     for( i = 0; i < 255; i++ )
401     {
402         p_dvbsub->p_clut[i] = NULL;
403     }
404     for( i = 0; i < 16; i++ )
405     {
406         p_dvbsub->p_spu[i] = NULL;
407     }
408     return 0;
409 }
410
411 static void free_all( dvbsub_all_t * );
412
413 static void dvbsub_clean( dvbsub_all_t *p_dvbsub )
414 {
415     free_all( p_dvbsub );
416     trox_call() ;
417 }
418
419
420 static void dvbsub_decode_clut( dvbsub_all_t *p_dvbsub, bs_t *s );
421 static void dvbsub_decode_page_composition( dvbsub_all_t *p_dvbsub, bs_t *s);
422 static void dvbsub_decode_region_composition( dvbsub_all_t *p_dvbsub, bs_t *s );
423 static void dvbsub_stop_display( dvbsub_all_t* p_dvbsub );
424 static void dvbsub_decode_object( dvbsub_all_t *p_dvbsub, bs_t *s );
425
426 static void free_page (dvbsub_page_t* p_p);
427
428 static void dvbsub_decode_segment( dvbsub_all_t *p_dvbspu, bs_t *s )
429 {
430     int i_type;
431     int i_page_id;
432     int i_size;
433
434     /* sync_byte */
435     bs_skip( s, 8 );
436
437     /* segment type */
438     i_type = bs_read( s, 8 );
439
440     /* page id */
441     i_page_id = bs_read( s, 16 );
442
443     /* segment size */
444     i_size = bs_show( s, 16 );
445
446     if( i_page_id != p_dvbspu->i_id )
447     {
448         bs_skip( s,  8 * ( 2 + i_size ) );
449         return;
450     }
451
452     switch( i_type )
453     {
454         case DVBSUB_ST_CLUT_DEFINITION:
455             dvbsub_decode_clut( p_dvbspu, s );
456             break;
457          case DVBSUB_ST_PAGE_COMPOSITION:
458             dvbsub_decode_page_composition( p_dvbspu, s );
459             break;
460         case DVBSUB_ST_REGION_COMPOSITION:
461             dvbsub_decode_region_composition( p_dvbspu, s );
462             break;
463         case DVBSUB_ST_OBJECT_DATA:
464             dvbsub_decode_object( p_dvbspu, s );
465             break;
466         case DVBSUB_ST_ENDOFDISPLAY:
467             dvbsub_stop_display( p_dvbspu );
468             break;
469         case DVBSUB_ST_STUFFING:
470         default:
471             fprintf( stderr, "*** DVBSUB - Unsupported segment type ! (%04x)",
472                       i_type );
473             bs_skip( s,  8 * ( 2 + i_size ) );
474             break;
475     }
476 }
477
478 static void dvbsub_stop_display( dvbsub_all_t *p_dvbsub )
479 {
480     int i;
481
482     for( i = 0; p_dvbsub->p_spu[i] != NULL; i++ )
483     {
484         p_dvbsub->p_spu[i]->i_stop = p_dvbsub->i_pts;
485     }
486 }
487
488 static void dvbsub_decode_clut( dvbsub_all_t *p_dvbsub, bs_t *s )
489 {
490     uint16_t         i_segment_length;
491     uint16_t         i_processed_length;
492     dvbsub_clut_t*   clut;
493     uint8_t          i_clut_id;
494     uint8_t          i_version_number;
495
496     i_segment_length = bs_read( s, 16 );
497     i_clut_id        = bs_read( s, 8 );
498     i_version_number = bs_read( s, 4 );
499
500     // Check that this id doesn't not already exist
501     // with the same version number
502     // And allocate memory if necessary
503     if( p_dvbsub->p_clut[i_clut_id] != NULL)
504     {
505         if( p_dvbsub->p_clut[i_clut_id]->i_version_number == i_version_number )
506         {
507             //TODO skip the right number of bits
508             return;
509         }
510         else
511         {
512             memset( p_dvbsub->p_clut[i_clut_id], 0, sizeof(dvbsub_clut_t) );
513         }
514     }
515     else
516     {
517         p_dvbsub->p_clut[i_clut_id] = trox_malloc( sizeof(dvbsub_clut_t) );
518     }
519     clut = p_dvbsub->p_clut[i_clut_id];
520
521     /* We don't have this version of the CLUT:
522      * 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 dvbsub_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 = trox_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
610      * a page with no regions is sent instead of a 0x80 packet (End Of Display) */
611     if( p_dvbsub->p_page->i_regions_number == 0 )
612     {
613         dvbsub_stop_display( p_dvbsub );
614     }
615     /* /Special workaround */
616
617     p_dvbsub->p_page->regions =
618                trox_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 dvbsub_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 = trox_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, uint16_t length );
730 static uint16_t dvbsub_count0x11( bs_t *s, uint16_t* p, dvbsub_image_t* p_image);
731
732 static void dvbsub_decode_object( dvbsub_all_t *p_dvbsub, bs_t *s )
733 {
734     dvbsub_object_t *p_obj;
735     uint16_t        i_segment_length;
736
737     /* Memory Allocation */
738     p_obj = trox_malloc ( sizeof ( dvbsub_object_t ) );
739     p_obj->p_next = NULL;
740
741     i_segment_length = bs_read( s, 16 );
742
743     /* TODO Check we don't already have this object / this version */
744     p_obj->i_id              =  bs_read( s, 16 );
745     p_obj->i_version_number  = bs_read( s, 4 );
746     p_obj->i_coding_method   = bs_read( s, 2 );
747     p_obj->b_non_modify_color= bs_read( s, 1 );
748     bs_skip( s, 1 ); /* Reserved */
749
750     if( p_obj->i_coding_method == 0x00 )
751     {
752         uint16_t    i_topfield_length;
753         uint16_t    i_bottomfield_length;
754
755         i_topfield_length   = bs_read( s, 16 );
756         i_bottomfield_length= bs_read( s, 16 );
757
758         p_obj->topfield    = dvbsub_parse_pdata( p_dvbsub, s, i_topfield_length );
759         p_obj->bottomfield = dvbsub_parse_pdata( p_dvbsub, s, i_bottomfield_length );
760     }
761     else
762     {
763         bs_skip( s, (i_segment_length - 3 ) * 8 );
764         /*TODO
765          * DVB subtitling as characters */
766     }
767
768     /* Add this object to the list of the page */
769     p_obj->p_next = p_dvbsub->p_objects;
770     p_dvbsub->p_objects = p_obj;
771 }
772
773 static dvbsub_image_t* dvbsub_parse_pdata( dvbsub_all_t *p_dvbsub, bs_t *s, uint16_t length )
774 {
775     dvbsub_image_t* p_image;
776     uint16_t i_processed_length=0;
777     uint16_t i_lines=0;
778     uint16_t i_cols_last=0;
779
780     p_image = trox_malloc ( sizeof ( dvbsub_image_t) );
781     p_image->p_last=NULL;
782
783     memset(p_image->i_cols, 0, 576*sizeof(uint16_t));
784
785     /* Let's parse it a first time to determine the size of the buffer */
786     while( i_processed_length < length)
787     {
788         switch( bs_read( s, 8 ) )
789         {
790             case 0x10:
791                 fprintf(stderr, "0x10 N/A\n");
792                 break;
793             case 0x11:
794                 i_processed_length +=
795                     1 + dvbsub_count0x11( s, &(p_image->i_cols[i_lines]),
796                                           p_image );
797                 break;
798             case 0x12:
799                 fprintf(stderr, "0x12 N/A\n");
800                 break;
801             case 0x20:
802                 fprintf(stderr, "0x20 N/A\n");
803                 break;
804             case 0x21:
805                 fprintf(stderr, "0x21 N/A\n");
806                 break;
807             case 0x22:
808                 fprintf(stderr, "0x22 N/A\n");
809                 break;
810             case 0xf0:
811                 i_processed_length++;
812                 i_lines++;
813                 break;
814         }
815     }
816
817     p_image->i_rows =  i_lines;
818     p_image->i_cols[i_lines] = i_cols_last;
819
820     /* Check word-aligned bits */
821     if( bs_show( s, 8 ) == 0x00 )
822     {
823         bs_skip( s, 8 );
824     }
825
826     return p_image;
827 }
828
829
830
831 static void add_rle_code( dvbsub_image_t *p, uint16_t num, uint8_t color )
832 {
833     if(p->p_last != NULL)
834     {
835         p->p_last->p_next = trox_malloc (sizeof (dvbsub_rle_t));
836         p->p_last = p->p_last->p_next;
837     }
838     else
839     {
840         p->p_codes =  trox_malloc (sizeof (dvbsub_rle_t));
841         p->p_last = p->p_codes;
842     }
843     p->p_last->i_num = num;
844     p->p_last->i_color_code = color;
845     p->p_last->p_next = NULL;
846 }
847
848
849 static uint16_t dvbsub_count0x11( bs_t *s, uint16_t* p, dvbsub_image_t* p_image )
850 {
851     uint16_t i_processed=0;
852     vlc_bool_t b_stop=0;
853     uint16_t i_count = 0;
854     uint8_t i_color =0;
855
856     while (!b_stop)
857     {
858         if( (i_color = bs_read( s, 4 )) != 0x00 )
859         {
860             (*p)++;
861             i_processed+=4;
862
863             /* 1 pixel of color code '0000' */
864             add_rle_code( p_image, 1, i_color );
865         }
866         else
867         {
868             if( bs_read( s, 1 ) == 0x00 )           // Switch1
869             {
870                 if( bs_show( s, 3 ) != 0x00 )
871                 {
872                     i_count = 2 + bs_read( s, 3 );
873                     (*p) += i_count ;
874                     add_rle_code( p_image, i_count, 0x00 );
875                 }
876                 else
877                 {
878                     bs_skip( s, 3 );
879                     b_stop=1;
880                 }
881                 i_processed += 8;
882             }
883             else
884             {
885                 if( bs_read( s, 1 ) == 0x00)        //Switch2
886                 {
887                     i_count =  4 + bs_read( s, 2 );
888                     i_color = bs_read( s, 4 );
889                     (*p) += i_count;
890                     i_processed += 12;
891                     add_rle_code( p_image, i_count, i_color );
892                 }
893                 else
894                 {
895                     switch ( bs_read( s, 2 ) )     //Switch3
896                     {
897                         case 0x0:
898                             (*p)++;
899                             i_processed += 8;
900                             add_rle_code( p_image, 1, 0x00 );
901                             break;
902                         case 0x1:
903                             (*p)+=2;
904                             i_processed += 8;
905                             add_rle_code( p_image, 2, 0x00 );
906                             break;
907                         case 0x2:
908                              i_count = 9 + bs_read( s, 4 );
909                              i_color = bs_read( s, 4 );
910                              (*p)+= i_count;
911                              i_processed += 16;
912                              add_rle_code( p_image, i_count, i_color );
913                              break;
914                         case 0x3:
915                              i_count= 25 + bs_read( s, 8 );
916                              i_color = bs_read( s, 4 );
917                              (*p)+= i_count;
918                              i_processed += 20;
919                              add_rle_code( p_image, i_count, i_color );
920                              break;
921                     }
922                 }
923             }
924         }
925     }
926
927     bs_align( s );
928
929     return ( i_processed + 7 ) / 8 ;
930 }
931
932 static void free_image (dvbsub_image_t* p_i)
933 {
934     dvbsub_rle_t* p1;
935     dvbsub_rle_t* p2=NULL;
936
937     for( p1 = p_i->p_codes; p1 != NULL; p1=p2)
938     {
939         p2=p1->p_next;
940         trox_free(p1);
941         p1=NULL;
942     }
943
944     trox_free(p_i);
945 }
946
947 static void free_object (dvbsub_object_t* p_o)
948 {
949     trox_free(p_o);
950 }
951
952 static void free_objectdefs ( dvbsub_objectdef_t* p_o)
953 {
954     dvbsub_objectdef_t* p1;
955     dvbsub_objectdef_t* p2=NULL;
956
957     for( p1 = p_o; p1 != NULL; p1=p2)
958     {
959         p2=p1->p_next;
960         trox_free(p1);
961         p1=NULL;
962     }
963 }
964
965 static void free_regions (dvbsub_region_t* p_r, uint8_t nb)
966 {
967     unsigned int i;
968
969     for (i = 0; i<nb; i++) free_objectdefs ( p_r[i].p_object );
970     trox_free (p_r);
971     p_r = NULL;
972 }
973
974 static void free_objects (dvbsub_object_t* p_o)
975 {
976     dvbsub_object_t* p1;
977     dvbsub_object_t* p2=NULL;
978
979     for( p1 = p_o; p1 != NULL; p1=p2)
980     {
981         p2=p1->p_next;
982         free_image (p1->topfield);
983         free_image (p1->bottomfield);
984         free_object(p1);
985     }
986 }
987
988 static void free_clut ( dvbsub_clut_t* p_c )
989 {
990     trox_free(p_c);
991 }
992
993 static void free_page (dvbsub_page_t* p_p)
994 {
995     free_regions (p_p->regions, p_p->i_regions_number);
996     trox_free(p_p);
997     p_p = NULL;
998 }
999
1000 static void free_spu( subpicture_t *p_spu )
1001 {
1002     if ( p_spu->p_sys )
1003     {
1004         free_image(((dvbsub_render_t *)p_spu->p_sys->p_data)->p_rle_top);
1005         free_image(((dvbsub_render_t *)p_spu->p_sys->p_data)->p_rle_bot);
1006         trox_free(p_spu->p_sys->p_data);
1007         trox_free( p_spu->p_sys );
1008         p_spu->p_sys = NULL;
1009     }
1010 }
1011
1012 static void free_all ( dvbsub_all_t* p_a )
1013 {
1014     unsigned int i;
1015
1016     for(i=0; i<0xff; i++) if (p_a->p_clut[i]) free_clut ( p_a->p_clut[i] );
1017     for(i=0; i<16; i++) if (p_a->p_spu[i]) free_spu ( p_a->p_spu[i] );
1018     if(p_a->p_page) free_page( p_a->p_page );
1019     free_objects (p_a->p_objects);
1020 }
1021
1022 static void RenderYUY2( vout_thread_t *p_vout, picture_t *p_pic,
1023                         const subpicture_t *p_spu )
1024 {
1025     /* Common variables */
1026     uint8_t  *p_desty;
1027     uint16_t i,j;
1028     uint16_t i_cnt;
1029     uint16_t x, y;
1030     dvbsub_rle_t* p_c;
1031     dvbsub_render_t* p_r = ((dvbsub_render_t *)p_spu->p_sys->p_data);
1032     dvbsub_image_t* p_im = p_r->p_rle_top;
1033     i=0;
1034     j=0;
1035     p_desty = p_pic->Y_PIXELS;
1036     //let's render the 1st frame
1037     for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1038     {
1039 //        if( p_c->y != 0  && p_c->t < 0x20)
1040         if( p_c->y != 0  && p_c->t < 0x20)
1041         {
1042             x = j+ p_r->i_x;
1043             y = 2*i+p_r->i_y;
1044             //memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1045             // In YUY2 we have to set pixel per pixel
1046             for( i_cnt = 0; i_cnt < p_c->i_num; i_cnt+=2 )
1047             {
1048                 memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt, p_c->y, 1);
1049            //     memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+1, p_c->cr, 1);
1050           //      memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+2, p_c->y, 1);
1051            //     memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+3, p_c->cb, 1);
1052             }
1053         }
1054         j += p_c->i_num;
1055         if(j >= p_im->i_cols[i])
1056         {
1057             i++; j=0;
1058         }
1059         if( i>= p_im->i_rows) break;
1060     }
1061     //idem for the second frame
1062     p_im = p_r->p_rle_bot; i=0; j=0;
1063     for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1064     {
1065         if( p_c->y != 0 && p_c->t < 0x20)
1066         {
1067             x = j+ p_r->i_x;
1068             y = 2*i+1+p_r->i_y;
1069             //memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1070             // In YUY2 we have to set pixel per pixel
1071             for( i_cnt = 0; i_cnt < p_c->i_num; i_cnt+=2 )
1072             {
1073                 memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt, p_c->y, 1);
1074            //     memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+1, p_c->cr, 1);
1075            //     memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+2, p_c->y, 1);
1076            //     memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+3, p_c->cb, 1);
1077            }
1078         }
1079         j += p_c->i_num;
1080         if(j >= p_im->i_cols[i])
1081         {
1082             i++; j=0;
1083         }
1084         if( i>= p_im->i_rows) break;
1085     }
1086 }
1087
1088
1089 static void RenderI42x( vout_thread_t *p_vout, picture_t *p_pic,
1090                         const subpicture_t *p_spu )
1091 {
1092     /* Common variables */
1093     uint8_t  *p_desty;
1094     uint8_t  *p_destu;
1095     uint8_t  *p_destv;
1096     uint16_t i,j;
1097     uint16_t x, y;
1098     dvbsub_rle_t* p_c;
1099     dvbsub_render_t* p_r = ((dvbsub_render_t *)p_spu->p_sys->p_data);
1100     dvbsub_image_t* p_im = p_r->p_rle_top;
1101     i=0;
1102     j=0;
1103     p_desty = p_pic->Y_PIXELS;
1104     p_destu = p_pic->U_PIXELS;
1105     p_destv = p_pic->V_PIXELS;
1106     //let's render the 1st frame
1107     for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1108     {
1109         if( p_c->y != 0 )
1110         {
1111             x = j+ p_r->i_x;
1112             y = 2*i+p_r->i_y;
1113             //memset(p_dest+ y*p_pic->U_PITCH*2 + x, p_c->cr, p_c->i_num);
1114 //            memset(p_desty+ (y)*p_pic->Y_PITCH + x, p_c->cr, p_c->i_num);
1115             //memset(p_dest+ y*p_pic->V_PITCH*2 + x, p_c->cb, p_c->i_num);
1116             //memset(p_destu+ (y)*p_pic->Y_PITCH + x, p_c->cb, p_c->i_num);
1117             memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1118   //          memset(p_desty+ 2*y*p_pic->U_PITCH + x, p_c->cr, p_c->i_num);
1119   //          memset(p_desty+ 2*y*p_pic->V_PITCH + x, p_c->cb, p_c->i_num);
1120         }
1121         j += p_c->i_num;
1122         if(j >= p_im->i_cols[i])
1123         {
1124             i++; j=0;
1125         }
1126         if( i>= p_im->i_rows) break;
1127     }
1128     //idem for the second frame
1129     p_im = p_r->p_rle_bot; i=0; j=0;
1130     for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1131     {
1132         if( p_c->y != 0 && p_c->t < 0x20)
1133         {
1134             x = j+ p_r->i_x;
1135             y = 2*i+1+p_r->i_y;
1136 //            memset(p_desty+ y*p_pic->U_PITCH*2 + x, p_c->cr, p_c->i_num);
1137 //            memset(p_desty+ y*p_pic->V_PITCH*2 + x, p_c->cb, p_c->i_num);
1138             memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1139 //            memset(p_desty+ 2*y*p_pic->U_PITCH + x, p_c->cr, p_c->i_num);
1140 //            memset(p_desty+ 2*y*p_pic->V_PITCH + x, p_c->cb, p_c->i_num);
1141         }
1142         j += p_c->i_num;
1143         if(j >= p_im->i_cols[i])
1144         {
1145             i++; j=0;
1146         }
1147         if( i>= p_im->i_rows) break;
1148     }
1149 }
1150
1151 static void dvbsub_RenderDVBSUB( vout_thread_t *p_vout, picture_t *p_pic,
1152                                  const subpicture_t *p_spu )
1153 {
1154     /* If we have changed the language on the fly */
1155
1156     if( p_spu->p_sys == NULL || p_spu->p_sys->b_obsolete )
1157     {
1158         return;
1159     }
1160
1161     switch( p_vout->output.i_chroma )
1162     {
1163         /* I420 target, no scaling */
1164         case VLC_FOURCC('I','4','2','2'):
1165         case VLC_FOURCC('I','4','2','0'):
1166         case VLC_FOURCC('I','Y','U','V'):
1167         case VLC_FOURCC('Y','V','1','2'):
1168             /* As long as we just use Y info, I422 and YV12 are just equivalent
1169              * to I420. Remember to change it the day we'll take into account
1170              * U and V info. */
1171             RenderI42x( p_vout, p_pic, p_spu );
1172             break;
1173
1174         /* RV16 target, scaling */
1175         case VLC_FOURCC('R','V','1','6'):
1176             fprintf(stderr, "Not implemented chroma ! RV16)\n");
1177             /* RenderRV16( p_vout, p_pic, p_spu ); */
1178             break;
1179
1180         /* RV32 target, scaling */
1181         case VLC_FOURCC('R','V','2','4'):
1182         case VLC_FOURCC('R','V','3','2'):
1183             fprintf(stderr, "Not implemented chroma ! RV32 \n");
1184             /* RenderRV32( p_vout, p_pic, p_spu ); */
1185             break;
1186
1187         /* NVidia overlay, no scaling */
1188         case VLC_FOURCC('Y','U','Y','2'):
1189             RenderYUY2( p_vout, p_pic, p_spu );
1190             break;
1191
1192         default:
1193             msg_Err( p_vout, "unknown chroma, can't render SPU" );
1194             break;
1195     }
1196 }
1197
1198 static void dvbsub_Destroy( subpicture_t *p_spu )
1199 {
1200     free_spu( p_spu );
1201 }
1202
1203 static void dvbsub_render( dvbsub_all_t *dvbsub, vout_thread_t *p_vout )
1204 {
1205     dvbsub_region_t*     p_region;
1206     dvbsub_objectdef_t*  p_objectdef;
1207     dvbsub_object_t*     p_o;
1208     dvbsub_object_t*     p_object;
1209     dvbsub_object_t*     p_object_old;
1210     dvbsub_render_t*     p_render;
1211     dvbsub_rle_t*        p_c;
1212     uint8_t i,j;
1213     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; p_objectdef = p_objectdef->p_next )
1222         {
1223             /* Look for the right object */
1224             p_object = dvbsub->p_objects;
1225             while((p_object!=NULL) && (p_object->i_id != p_objectdef->i_id))
1226             {
1227                 p_object = p_object->p_next;
1228             }
1229
1230             if(p_object==NULL)
1231             {
1232                 fprintf(stderr, "Internal DvbSub decoder error\n");
1233                 return;
1234             }
1235
1236             /* Allocate the render structure */
1237             p_render = trox_malloc(sizeof(dvbsub_render_t));
1238             p_render->i_x = p_region->i_x + p_objectdef->i_xoffset;
1239             p_render->i_y = p_region->i_y + p_objectdef->i_yoffset;
1240             p_render->p_rle_top = p_object->topfield;
1241             p_render->p_rle_bot = p_object->bottomfield;
1242
1243             // if we did not recieved the CLUT yet
1244             if ( !dvbsub->p_clut[p_region->i_clut] ) return;
1245
1246             /* Compute the color datas according to the appropriate CLUT */
1247             for(p_c=p_render->p_rle_top->p_codes;p_c->p_next!=NULL; p_c=p_c->p_next)
1248             {
1249                 //TODO We assume here we are working in 4bp
1250                 p_c->y=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Y;
1251                 p_c->cr=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Cr;
1252                 p_c->cb=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Cb;
1253                 p_c->t=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].T;
1254             }
1255             for(p_c=p_render->p_rle_bot->p_codes;p_c->p_next!=NULL; p_c=p_c->p_next)
1256             {
1257                 //TODO We assume here we are working in 4bp
1258                 p_c->y=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Y;
1259                 p_c->cr=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Cr;
1260                 p_c->cb=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Cb;
1261                 p_c->t=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].T;
1262             }
1263
1264
1265             /* Allocate the subpicture internal data. */
1266             dvbsub->p_spu[j] = vout_CreateSubPicture( p_vout, MEMORY_SUBPICTURE );
1267             if( dvbsub->p_spu[j] == NULL )
1268             {
1269                 fprintf(stderr, "Unable to allocate memory ... skipping\n");
1270                 return;
1271             }
1272             /* Set the pf_render callback */
1273             dvbsub->p_spu[j]->pf_render = dvbsub_RenderDVBSUB;
1274             dvbsub->p_spu[j]->p_sys =  trox_malloc( sizeof( subpicture_sys_t ));
1275             dvbsub->p_spu[j]->p_sys->p_data = p_render;
1276             dvbsub->p_spu[j]->p_sys->b_obsolete=0;
1277             dvbsub->p_spu[j]->pf_destroy = dvbsub_Destroy;
1278             dvbsub->p_spu[j]->i_start = dvbsub->i_pts;
1279             dvbsub->p_spu[j]->i_stop =  dvbsub->p_spu[j]->i_start + dvbsub->p_page->i_timeout*1000000;
1280             dvbsub->p_spu[j]->b_ephemer = VLC_FALSE;
1281
1282             // At this stage, we have all we need in p_render
1283             // We need to free the object
1284             //Remove this object from the list
1285             p_object_old = p_object;
1286             if(p_object == dvbsub->p_objects)
1287               dvbsub->p_objects = p_object->p_next;
1288             else
1289             {
1290              for(p_o = dvbsub->p_objects; p_o->p_next != p_object; 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 }
1301