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