]> git.sesse.net Git - vlc/blob - modules/codec/dvbsub.c
* modules/codec/dvbsub.c: rewrite of the DVB subtitles decoder.
[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 *p_this );
41 static void Close( vlc_object_t *p_this );
42
43 vlc_module_begin();
44     set_description( _("DVB subtitles decoder") );
45     set_capability( "decoder", 50 );
46     set_callbacks( Open, Close );
47 vlc_module_end();
48
49 /****************************************************************************
50  * Local structures
51  ****************************************************************************
52  * Those structures refer closely to the ETSI 300 743 Object model
53  ****************************************************************************/
54
55 /* Storage of a RLE entry */
56 typedef struct dvbsub_rle_s
57 {
58     uint16_t                i_num;
59     int                     i_color_code;
60     int                     i_bpp;
61     uint8_t                 y;
62     uint8_t                 cr;
63     uint8_t                 cb;
64     uint8_t                 t;
65     struct dvbsub_rle_s     *p_next;
66
67 } dvbsub_rle_t;
68
69 /* A subpicture image is a list of codes
70  * We need to store the length of each line since nothing specify in
71  * the standard that all lines should have the same length
72  * WARNING: We assume here that a spu is less than 576 lines high */
73 typedef struct
74 {
75     uint16_t                i_rows;
76     uint16_t                i_cols[576];
77     dvbsub_rle_t            *p_last;
78     dvbsub_rle_t            *p_codes;
79
80 } dvbsub_image_t;
81
82 /* The object definition gives the position of the object in a region */
83 typedef struct dvbsub_objectdef_s
84 {
85     uint16_t                  i_id;
86     uint8_t                   i_type;
87     uint8_t                   i_provider;
88     uint16_t                  i_x;
89     uint16_t                  i_y;
90     uint8_t                   i_fg_pc;
91     uint8_t                   i_bg_pc;
92
93 } dvbsub_objectdef_t;
94
95 /* An object is constituted of 2 images (for interleaving) */
96 typedef struct dvbsub_object_s
97 {
98     uint16_t                i_id;
99     uint8_t                 i_version_number;
100     uint8_t                 i_coding_method;
101     vlc_bool_t              b_non_modify_color;
102     dvbsub_image_t         *topfield;
103     dvbsub_image_t         *bottomfield;
104     struct dvbsub_object_s *p_next;
105
106 } dvbsub_object_t;
107
108 /* The object definition gives the position of the object in a region */
109 typedef struct dvbsub_regiondef_s
110 {
111     uint16_t                  i_id;
112     uint16_t                  i_x;
113     uint16_t                  i_y;
114
115 } dvbsub_regiondef_t;
116
117 /* The Region is an aera on the image
118  * with a list of the object definitions associated 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
135     int                     i_object_defs;
136     dvbsub_objectdef_t      *p_object_defs;
137
138     struct dvbsub_region_s  *p_next;
139
140 } dvbsub_region_t;
141
142 /* The page defines the list of regions */
143 typedef struct
144 {
145     uint16_t              i_id;
146     uint8_t               i_timeout;
147     uint8_t               i_state;
148     uint8_t               i_version_number;
149
150     uint8_t               i_region_defs;
151     dvbsub_regiondef_t    *p_region_defs;
152
153 } dvbsub_page_t;
154
155 /* The entry in the palette CLUT */
156 typedef struct
157 {
158     uint8_t                 Y;
159     uint8_t                 Cr;
160     uint8_t                 Cb;
161     uint8_t                 T;
162
163 } dvbsub_color_t;
164
165 /* */
166 typedef struct
167 {
168     uint8_t                 i_id;
169     uint8_t                 i_version_number;
170     dvbsub_color_t          c_2b[4];
171     dvbsub_color_t          c_4b[16];
172     dvbsub_color_t          c_8b[256];
173
174 } dvbsub_clut_t;
175
176 typedef struct dvbsub_render_s
177 {
178     uint16_t               i_x;
179     uint16_t               i_y;
180     dvbsub_image_t         *p_rle_top;
181     dvbsub_image_t         *p_rle_bot;
182
183     struct dvbsub_render_s *p_next;
184
185 } dvbsub_render_t;
186
187 struct subpicture_sys_t
188 {
189     dvbsub_render_t         *p_objects;  /* Linked list of objects to render */
190 };
191
192 struct decoder_sys_t
193 {
194     vout_thread_t   *p_vout;
195
196     bs_t            bs;
197
198     /* Decoder internal data */
199     int i_id;
200     int i_ancillary_id;
201
202     mtime_t i_pts;
203
204     dvbsub_page_t   *p_page;
205     dvbsub_region_t *p_regions;
206     dvbsub_object_t *p_objects;
207
208     dvbsub_clut_t   *p_clut[256];
209     dvbsub_clut_t   default_clut;
210
211     subpicture_t    *p_spu;
212     int             i_subpic_channel;
213 };
214
215
216 // List of different SEGMENT TYPES
217 // According to EN 300-743, table 2
218 #define DVBSUB_ST_PAGE_COMPOSITION      0x10
219 #define DVBSUB_ST_REGION_COMPOSITION    0x11
220 #define DVBSUB_ST_CLUT_DEFINITION       0x12
221 #define DVBSUB_ST_OBJECT_DATA           0x13
222 #define DVBSUB_ST_ENDOFDISPLAY          0x80
223 #define DVBSUB_ST_STUFFING              0xff
224 // List of different OBJECT TYPES
225 // According to EN 300-743, table 6
226 #define DVBSUB_OT_BASIC_BITMAP          0x00
227 #define DVBSUB_OT_BASIC_CHAR            0x01
228 #define DVBSUB_OT_COMPOSITE_STRING      0x02
229 // Pixel DATA TYPES
230 // According to EN 300-743, table 9
231 #define DVBSUB_DT_2BP_CODE_STRING       0x10
232 #define DVBSUB_DT_4BP_CODE_STRING       0x11
233 #define DVBSUB_DT_8BP_CODE_STRING       0x12
234 #define DVBSUB_DT_24_TABLE_DATA         0x20
235 #define DVBSUB_DT_28_TABLE_DATA         0x21
236 #define DVBSUB_DT_48_TABLE_DATA         0x22
237 #define DVBSUB_DT_END_LINE              0xf0
238 // List of different Page Composition Segment state
239 // According to EN 300-743, 7.2.1 table 3
240 #define DVBSUB_PCS_STATE_ACQUISITION    0x01
241 #define DVBSUB_PCS_STATE_CHANGE         0x10
242
243 /*****************************************************************************
244  * Local prototypes
245  *****************************************************************************/
246 static void Decode( decoder_t *, block_t ** );
247
248 static void decode_segment( decoder_t *, bs_t * );
249 static void decode_page_composition( decoder_t *, bs_t * );
250 static void decode_region_composition( decoder_t *, bs_t * );
251 static void decode_object( decoder_t *, bs_t * );
252 static void decode_clut( decoder_t *, bs_t * );
253
254 static void free_objects( decoder_t * );
255 static void free_all( decoder_t * );
256
257 static void render( decoder_t *, vout_thread_t * );
258 static void default_clut_init( decoder_t * );
259
260 /*****************************************************************************
261  * Open: probe the decoder and return score
262  *****************************************************************************
263  * Tries to launch a decoder and return score so that the interface is able
264  * to chose.
265  *****************************************************************************/
266 static int Open( vlc_object_t *p_this )
267 {
268     decoder_t     *p_dec = (decoder_t *) p_this;
269     decoder_sys_t *p_sys;
270     int i;
271
272     if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','v','b','s') )
273     {
274         return VLC_EGENERIC;
275     }
276
277     p_dec->pf_decode_sub = Decode;
278     p_sys = p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
279
280     p_sys->i_pts          = 0;
281     p_sys->i_id           = p_dec->fmt_in.subs.dvb.i_id & 0xFFFF;
282     p_sys->i_ancillary_id = p_dec->fmt_in.subs.dvb.i_id >> 16;
283     p_sys->p_page         = NULL;
284     p_sys->p_regions      = NULL;
285     p_sys->p_objects      = NULL;
286     p_sys->p_vout         = NULL;
287     p_sys->p_spu          = NULL;
288     for( i = 0; i < 256; i++ ) p_sys->p_clut[i] = NULL;
289
290     es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'd','v','b','s' ) );
291
292     default_clut_init( p_dec );
293
294     return VLC_SUCCESS;
295 }
296
297 /*****************************************************************************
298  * Close:
299  *****************************************************************************/
300 static void Close( vlc_object_t *p_this )
301 {
302     decoder_t     *p_dec = (decoder_t*) p_this;
303     decoder_sys_t *p_sys = p_dec->p_sys;
304
305     if( p_sys->p_vout && p_sys->p_vout->p_subpicture != NULL )
306     {
307         subpicture_t *p_subpic;
308         int i_subpic;
309         for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
310         {
311             p_subpic = &p_sys->p_vout->p_subpicture[i_subpic];
312             if( p_subpic != NULL &&
313                 ( p_subpic->i_status == RESERVED_SUBPICTURE ||
314                   p_subpic->i_status == READY_SUBPICTURE ) )
315             {
316                 vout_DestroySubPicture( p_sys->p_vout, p_subpic );
317             }
318         }
319     }
320
321     free_all( p_dec );
322     free( p_sys );
323 }
324
325 /*****************************************************************************
326  * Decode:
327  *****************************************************************************/
328 static void Decode( decoder_t *p_dec, block_t **pp_block )
329 {
330     decoder_sys_t *p_sys = p_dec->p_sys;
331     block_t       *p_block;
332     vout_thread_t *p_last_vout;
333
334     if( pp_block == NULL || *pp_block == NULL )
335     {
336         return;
337     }
338     p_block = *pp_block;
339     *pp_block = NULL;
340
341     p_sys->i_pts = p_block->i_pts;
342     if( p_sys->i_pts <= 0 )
343     {
344         msg_Warn( p_dec, "non dated subtitle" );
345         block_Release( p_block );
346         return;
347     }
348
349     bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
350
351     if( bs_read( &p_sys->bs, 8 ) != 0x20 ) /* Data identifier */
352     {
353         msg_Dbg( p_dec, "invalid data identifier" );
354         block_Release( p_block );
355         return;
356     }
357
358     if( bs_read( &p_sys->bs, 8 ) != 0x20 && 0 ) /* Subtitle stream id */
359     {
360         msg_Dbg( p_dec, "invalid subtitle stream id" );
361         block_Release( p_block );
362         return;
363     }
364
365     while( bs_show( &p_sys->bs, 8 ) == 0x0f ) /* Sync byte */
366     {
367         decode_segment( p_dec, &p_sys->bs );
368     }
369
370     if( bs_read( &p_sys->bs, 8 ) != 0xff ) /* End marker */
371     {
372         msg_Warn( p_dec, "end marker not found (corrupted subtitle ?)" );
373         block_Release( p_block );
374         return;
375     }
376
377     p_last_vout = p_sys->p_vout;
378     if( ( p_sys->p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT,
379                                            FIND_ANYWHERE ) ) )
380     {
381         if( p_last_vout != p_sys->p_vout )
382         {
383             p_sys->i_subpic_channel =
384                 vout_RegisterOSDChannel( p_sys->p_vout );
385         }
386
387         /* Check if the page is to be displayed */
388         if( p_sys->p_page ) render( p_dec, p_sys->p_vout );
389
390         vlc_object_release( p_sys->p_vout );
391     }
392 #ifdef DEBUG_DVBSUB
393     else if( p_sys->p_page ) render( p_dec, NULL );
394 #endif
395
396     block_Release( p_block );
397 }
398
399 /* following functions are local */
400
401 /*****************************************************************************
402  * default_clut_init: default clut as defined in EN 300-743 section 10
403  *****************************************************************************/
404 static void default_clut_init( decoder_t *p_dec )
405 {
406     decoder_sys_t *p_sys = p_dec->p_sys;
407     uint8_t i;
408
409 #define RGB_TO_Y(r, g, b) ((int16_t) 77 * r + 150 * g + 29 * b) / 256;
410 #define RGB_TO_U(r, g, b) ((int16_t) -44 * r - 87 * g + 131 * b) / 256;
411 #define RGB_TO_V(r, g, b) ((int16_t) 131 * r - 110 * g - 21 * b) / 256;
412
413     /* 4 entries CLUT */
414     for( i = 0; i < 4; i++ )
415     {
416         uint8_t R = 0, G = 0, B = 0, T = 0;
417
418         if( !(i & 0x2) && !(i & 0x1) ) T = 0xFF;
419         else if( !(i & 0x2) && (i & 0x1) ) R = G = B = 0xFF;
420         else if( (i & 0x2) && !(i & 0x1) ) R = G = B = 0;
421         else R = G = B = 0x7F;
422
423         p_sys->default_clut.c_2b[i].Y = RGB_TO_Y(R,G,B);
424         p_sys->default_clut.c_2b[i].Cr = RGB_TO_U(R,G,B);
425         p_sys->default_clut.c_2b[i].Cb = RGB_TO_V(R,G,B);
426         p_sys->default_clut.c_2b[i].T = T;
427     }
428
429     /* 16 entries CLUT */
430     for( i = 0; i < 16; i++ )
431     {
432         uint8_t R = 0, G = 0, B = 0, T = 0;
433
434         if( !(i & 0x8) )
435         {
436             if( !(i & 0x4) && !(i & 0x2) && !(i & 0x1) )
437             {
438                 T = 0xFF;
439             }
440             else
441             {
442                 R = (i & 0x1) ? 0xFF : 0;
443                 G = (i & 0x2) ? 0xFF : 0;
444                 B = (i & 0x4) ? 0xFF : 0;
445             }
446         }
447         else
448         {
449             R = (i & 0x1) ? 0x7F : 0;
450             G = (i & 0x2) ? 0x7F : 0;
451             B = (i & 0x4) ? 0x7F : 0;
452         }
453
454         p_sys->default_clut.c_4b[i].Y = RGB_TO_Y(R,G,B);
455         p_sys->default_clut.c_4b[i].Cr = RGB_TO_U(R,G,B);
456         p_sys->default_clut.c_4b[i].Cb = RGB_TO_V(R,G,B);
457         p_sys->default_clut.c_4b[i].T = T;
458     }
459
460     /* 256 entries CLUT (TODO) */
461     memset( p_sys->default_clut.c_8b, 0xFF, 256 * sizeof(dvbsub_color_t) );
462 }
463
464 static void decode_segment( decoder_t *p_dec, bs_t *s )
465 {
466     decoder_sys_t *p_sys = p_dec->p_sys;
467     int i_type;
468     int i_page_id;
469     int i_size;
470
471     /* sync_byte (already checked) */
472     bs_skip( s, 8 );
473
474     /* segment type */
475     i_type = bs_read( s, 8 );
476
477     /* page id */
478     i_page_id = bs_read( s, 16 );
479
480     /* segment size */
481     i_size = bs_show( s, 16 );
482
483     if( i_page_id != p_sys->i_id && i_page_id != p_sys->i_ancillary_id )
484     {
485 #ifdef DEBUG_DVBSUB
486         msg_Dbg( p_dec, "subtitle skipped (page id: %i)", i_page_id );
487 #endif
488         bs_skip( s,  8 * ( 2 + i_size ) );
489         return;
490     }
491
492 #ifdef DEBUG_DVBSUB
493     if( i_page_id == p_sys->i_id )
494         msg_Dbg( p_dec, "segment (id: %i)", i_page_id );
495     else
496         msg_Dbg( p_dec, "ancillary segment (id: %i)", i_page_id );
497 #endif
498
499     switch( i_type )
500     {
501     case DVBSUB_ST_PAGE_COMPOSITION:
502 #ifdef DEBUG_DVBSUB
503         msg_Dbg( p_dec, "decode_page_composition" );
504 #endif
505         decode_page_composition( p_dec, s );
506         break;
507
508     case DVBSUB_ST_REGION_COMPOSITION:
509 #ifdef DEBUG_DVBSUB
510         msg_Dbg( p_dec, "decode_region_composition" );
511 #endif
512         decode_region_composition( p_dec, s );
513         break;
514
515     case DVBSUB_ST_CLUT_DEFINITION:
516 #ifdef DEBUG_DVBSUB
517         msg_Dbg( p_dec, "decode_clut" );
518 #endif
519         decode_clut( p_dec, s );
520         break;
521
522     case DVBSUB_ST_OBJECT_DATA:
523 #ifdef DEBUG_DVBSUB
524         msg_Dbg( p_dec, "decode_object" );
525 #endif
526         decode_object( p_dec, s );
527         break;
528
529     case DVBSUB_ST_ENDOFDISPLAY:
530 #ifdef DEBUG_DVBSUB
531         msg_Dbg( p_dec, "end of display" );
532 #endif
533         bs_skip( s,  8 * ( 2 + i_size ) );
534         break;
535
536     case DVBSUB_ST_STUFFING:
537 #ifdef DEBUG_DVBSUB
538         msg_Dbg( p_dec, "skip stuffing" );
539 #endif
540         bs_skip( s,  8 * ( 2 + i_size ) );
541         break;
542
543     default:
544         msg_Warn( p_dec, "unsupported segment type: (%04x)", i_type );
545         bs_skip( s,  8 * ( 2 + i_size ) );
546         break;
547     }
548 }
549
550 static void decode_clut( decoder_t *p_dec, bs_t *s )
551 {
552     decoder_sys_t *p_sys = p_dec->p_sys;
553     uint16_t      i_segment_length;
554     uint16_t      i_processed_length;
555     dvbsub_clut_t *p_clut;
556     uint8_t       i_clut_id;
557     uint8_t       i_version_number;
558
559     i_segment_length = bs_read( s, 16 );
560     i_clut_id        = bs_read( s, 8 );
561     i_version_number = bs_read( s, 4 );
562
563     /* Check that this id doesn't not already exist with the same version
564      * number and allocate memory if necessary */
565     if( p_sys->p_clut[i_clut_id] != NULL &&
566         p_sys->p_clut[i_clut_id]->i_version_number == i_version_number )
567     {
568         /* Nothing to do */
569         bs_skip( s,  8 * i_segment_length - 12 );
570         return;
571     }
572
573     if( !p_sys->p_clut[i_clut_id] )
574     {
575         p_sys->p_clut[i_clut_id] = malloc( sizeof(dvbsub_clut_t) );
576     }
577
578     p_clut = p_sys->p_clut[i_clut_id];
579
580     /* We don't have this version of the CLUT: Parse it */
581     p_clut->i_version_number = i_version_number;
582     bs_skip( s, 4 ); /* Reserved bits */
583     i_processed_length = 2;
584     while( i_processed_length < i_segment_length )
585     {
586         uint8_t y, cb, cr, t;
587         uint8_t i_id;
588         uint8_t i_type;
589
590         i_id = bs_read( s, 8 );
591         i_type = bs_read( s, 3 );
592
593         bs_skip( s, 4 );
594
595         if( bs_read( s, 1 ) )
596         {
597             y  = bs_read( s, 8 );
598             cr = bs_read( s, 8 );
599             cb = bs_read( s, 8 );
600             t  = bs_read( s, 8 );
601             i_processed_length += 6;
602         }
603         else
604         {
605             y  = bs_read( s, 6 );
606             cr = bs_read( s, 4 );
607             cb = bs_read( s, 4 );
608             t  = bs_read( s, 2 );
609             i_processed_length += 4;
610         }
611
612         /* According to EN 300-743 section 7.2.3 note 1, type should
613          * not have more than 1 bit set to one, but some strams don't
614          * respect this note. */
615
616         if( i_type&0x04)
617         {
618             p_clut->c_2b[i_id].Y = y;
619             p_clut->c_2b[i_id].Cr = cr;
620             p_clut->c_2b[i_id].Cb = cb;
621             p_clut->c_2b[i_id].T = t;
622         }
623         if( i_type&0x02)
624         {
625             p_clut->c_4b[i_id].Y = y;
626             p_clut->c_4b[i_id].Cr = cr;
627             p_clut->c_4b[i_id].Cb = cb;
628             p_clut->c_4b[i_id].T = t;
629         }
630         if( i_type & 0x01)
631         {
632             p_clut->c_8b[i_id].Y = y;
633             p_clut->c_8b[i_id].Cr = cr;
634             p_clut->c_8b[i_id].Cb = cb;
635             p_clut->c_8b[i_id].T = t;
636         }
637     }
638 }
639
640 static void decode_page_composition( decoder_t *p_dec, bs_t *s )
641 {
642     decoder_sys_t *p_sys = p_dec->p_sys;
643     unsigned int i_version_number;
644     unsigned int i_state;
645     unsigned int i_segment_length;
646     uint8_t i_timeout;
647     unsigned int i;
648
649     /* A page is composed by one or more region */
650
651     i_segment_length = bs_read( s, 16 );
652     i_timeout = bs_read( s, 8 );
653     i_version_number = bs_read( s, 4 );
654     i_state = bs_read( s, 2 );
655     bs_skip( s, 2 ); /* Reserved */
656
657     if( i_state == DVBSUB_PCS_STATE_CHANGE )
658     {
659         /* End of an epoch, reset decoder buffer */
660 #ifdef DEBUG_DVBSUB
661         msg_Dbg( p_dec, "page composition mode change" );
662 #endif
663         free_all( p_dec );
664     }
665     else if( !p_sys->p_page && i_state != DVBSUB_PCS_STATE_ACQUISITION )
666     {
667         /* Not a full PCS, we need to wait for one */
668         return;
669     }
670
671     if( i_state == DVBSUB_PCS_STATE_ACQUISITION )
672     {
673         /* Make sure we clean up regularly our objects list.
674          * Is it the best place to do this ? */
675         free_objects( p_dec );
676     }
677
678     /* Check version number */
679     if( p_sys->p_page &&
680         p_sys->p_page->i_version_number == i_version_number )
681     {
682         bs_skip( s,  8 * (i_segment_length - 2) );
683         return;
684     }
685     else if( p_sys->p_page )
686     {
687         if( p_sys->p_page->i_region_defs )
688             free( p_sys->p_page->p_region_defs );
689         p_sys->p_page->i_region_defs = 0;
690     }
691
692     if( !p_sys->p_page )
693     {
694         /* Allocate a new page */
695         p_sys->p_page = malloc( sizeof(dvbsub_page_t) );
696     }
697
698     p_sys->p_page->i_version_number = i_version_number;
699     p_sys->p_page->i_timeout = i_timeout;
700
701     /* Number of regions */
702     p_sys->p_page->i_region_defs = (i_segment_length - 2) / 6;
703
704     if( p_sys->p_page->i_region_defs == 0 ) return;
705
706     p_sys->p_page->p_region_defs =
707         malloc( p_sys->p_page->i_region_defs * sizeof(dvbsub_region_t) );
708     for( i = 0; i < p_sys->p_page->i_region_defs; i++ )
709     {
710         p_sys->p_page->p_region_defs[i].i_id = bs_read( s, 8 );
711         bs_skip( s, 8 ); /* Reserved */
712         p_sys->p_page->p_region_defs[i].i_x = bs_read( s, 16 );
713         p_sys->p_page->p_region_defs[i].i_y = bs_read( s, 16 );
714
715 #ifdef DEBUG_DVBSUB
716         msg_Dbg( p_dec, "page_composition, region %i (%i,%i)",
717                  i, p_sys->p_page->p_region_defs[i].i_x,
718                  p_sys->p_page->p_region_defs[i].i_y );
719 #endif
720     }
721 }
722
723 static void decode_region_composition( decoder_t *p_dec, bs_t *s )
724 {
725     decoder_sys_t *p_sys = p_dec->p_sys;
726     dvbsub_region_t *p_region, **pp_region = &p_sys->p_regions;
727     int i_segment_length;
728     int i_processed_length;
729     int i_region_id;
730     int i_version_number;
731
732     i_segment_length = bs_read( s, 16 );
733     i_region_id = bs_read( s, 8 );
734     i_version_number = bs_read( s, 4 );
735
736     /* Check if we already have this region */
737     for( p_region = p_sys->p_regions; p_region != NULL;
738          p_region = p_region->p_next )
739     {
740         pp_region = &p_region->p_next;
741         if( p_region->i_id == i_region_id ) break;
742     }
743
744     /* Check version number */
745     if( p_region &&
746         p_region->i_version_number == i_version_number )
747     {
748         bs_skip( s,  8 * (i_segment_length - 1) - 4 );
749         return;
750     }
751     else if( p_region )
752     {
753         if( p_region->i_object_defs )
754             free( p_region->p_object_defs );
755     }
756
757     if( !p_region )
758     {
759 #ifdef DEBUG_DVBSUB
760         msg_Dbg( p_dec, "new region: %i", i_region_id );
761 #endif
762         p_region = *pp_region = malloc( sizeof(dvbsub_region_t) );
763         p_region->p_next = NULL;
764     }
765
766     /* Region attributes */
767     p_region->i_id = i_region_id;
768     p_region->i_version_number = i_version_number;
769     p_region->b_fill           = bs_read( s, 1 );
770     bs_skip( s, 3 ); /* Reserved */
771     p_region->i_width          = bs_read( s, 16 );
772     p_region->i_height         = bs_read( s, 16 );
773     p_region->i_level_comp     = bs_read( s, 3 );
774     p_region->i_depth          = bs_read( s, 3 );
775     bs_skip( s, 2 ); /* Reserved */
776     p_region->i_clut           = bs_read( s, 8 );
777     p_region->i_8bp_code       = bs_read( s, 8 );
778     p_region->i_4bp_code       = bs_read( s, 4 );
779     p_region->i_2bp_code       = bs_read( s, 2 );
780     bs_skip( s, 2 ); /* Reserved */
781     p_region->p_object_defs    = NULL;
782     p_region->i_object_defs    = 0;
783
784     /* List of objects in the region */
785     i_processed_length = 10;
786     while( i_processed_length < i_segment_length )
787     {
788         dvbsub_objectdef_t *p_obj;
789
790         /* We create a new object */
791         p_region->i_object_defs++;
792         p_region->p_object_defs =
793             realloc( p_region->p_object_defs,
794                      sizeof(dvbsub_objectdef_t) * p_region->i_object_defs );
795
796         /* We parse object properties */
797         p_obj = &p_region->p_object_defs[p_region->i_object_defs - 1];
798         p_obj->i_id         = bs_read( s, 16 );
799         p_obj->i_type       = bs_read( s, 2 );
800         p_obj->i_provider   = bs_read( s, 2 );
801         p_obj->i_x          = bs_read( s, 12 );
802         bs_skip( s, 4 ); /* Reserved */
803         p_obj->i_y          = bs_read( s, 12 );
804
805         i_processed_length += 6;
806
807         if( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ||
808             p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING )
809         {
810             p_obj->i_fg_pc =  bs_read( s, 8 );
811             p_obj->i_bg_pc =  bs_read( s, 8 );
812             i_processed_length += 2;
813         }
814     }
815 }
816
817 static dvbsub_image_t *dvbsub_parse_pdata( decoder_t *, bs_t *, uint16_t );
818 static uint16_t dvbsub_pdata2bpp( bs_t *, uint16_t *, dvbsub_image_t * );
819 static uint16_t dvbsub_pdata4bpp( bs_t *, uint16_t *, dvbsub_image_t * );
820 static uint16_t dvbsub_pdata8bpp( bs_t *, uint16_t *, dvbsub_image_t * );
821
822 static void decode_object( decoder_t *p_dec, bs_t *s )
823 {
824     decoder_sys_t   *p_sys = p_dec->p_sys;
825     dvbsub_object_t *p_obj, **pp_obj = &p_sys->p_objects;
826     int i_segment_length;
827     int i_version_number;
828     int i_coding_method;
829     int i_obj_id;
830
831     i_segment_length   = bs_read( s, 16 );
832     i_obj_id           = bs_read( s, 16 );
833     i_version_number   = bs_read( s, 4 );
834     i_coding_method    = bs_read( s, 2 );
835
836     if( i_coding_method )
837     {
838         /* TODO: DVB subtitling as characters */
839         msg_Dbg( p_dec, "DVB subtitling as characters is not handled!" );
840         bs_skip( s,  8 * (i_segment_length - 2) - 6 );
841         return;
842     }
843
844     /* Check if we already have this region */
845     for( p_obj = p_sys->p_objects; p_obj != NULL; p_obj = p_obj->p_next )
846     {
847         pp_obj = &p_obj->p_next;
848         if( p_obj->i_id == i_obj_id ) break;
849     }
850
851     /* Check version number */
852     if( p_obj && p_obj->i_version_number == i_version_number )
853     {
854         bs_skip( s,  8 * (i_segment_length - 2) - 6 );
855         return;
856     }
857     else if( p_obj )
858     {
859         /* Clean structure */
860     }
861
862     if( !p_obj )
863     {
864 #ifdef DEBUG_DVBSUB
865         msg_Dbg( p_dec, "new object: %i", i_obj_id );
866 #endif
867         p_obj = *pp_obj = malloc( sizeof(dvbsub_object_t) );
868         p_obj->p_next = NULL;
869     }
870
871     p_obj->i_id               = i_obj_id;
872     p_obj->i_version_number   = i_version_number;
873     p_obj->i_coding_method    = i_coding_method;
874     p_obj->b_non_modify_color = bs_read( s, 1 );
875     bs_skip( s, 1 ); /* Reserved */
876
877     if( p_obj->i_coding_method == 0x00 )
878     {
879         uint16_t i_topfield_length;
880         uint16_t i_bottomfield_length;
881
882         i_topfield_length    = bs_read( s, 16 );
883         i_bottomfield_length = bs_read( s, 16 );
884
885         p_obj->topfield =
886             dvbsub_parse_pdata( p_dec, s, i_topfield_length );
887         p_obj->bottomfield =
888             dvbsub_parse_pdata( p_dec, s, i_bottomfield_length );
889     }
890     else
891     {
892         /* TODO: DVB subtitling as characters */
893     }
894 }
895
896 static dvbsub_image_t* dvbsub_parse_pdata( decoder_t *p_dec, bs_t *s,
897                                            uint16_t length )
898 {
899     dvbsub_image_t* p_image;
900     uint16_t i_processed_length = 0;
901     uint16_t i_lines = 0;
902     uint16_t i_cols_last = 0;
903
904     p_image = malloc( sizeof(dvbsub_image_t) );
905     p_image->p_last = NULL;
906
907     memset( p_image->i_cols, 0, 576 * sizeof(uint16_t) );
908
909     /* Let's parse it a first time to determine the size of the buffer */
910     while( i_processed_length < length)
911     {
912         i_processed_length++;
913
914         switch( bs_read( s, 8 ) )
915         {
916             case 0x10:
917                 i_processed_length +=
918                     dvbsub_pdata2bpp( s, &p_image->i_cols[i_lines], p_image );
919                 break;
920             case 0x11:
921                 i_processed_length +=
922                     dvbsub_pdata4bpp( s, &p_image->i_cols[i_lines], p_image );
923                 break;
924             case 0x12:
925                 i_processed_length +=
926                     dvbsub_pdata8bpp( s, &p_image->i_cols[i_lines], p_image );
927                 break;
928             case 0x20:
929             case 0x21:
930             case 0x22:
931                 /* We don't use map tables */
932                 break;
933             case 0xf0:
934                 i_lines++; /* End of line code */
935                 break;
936         }
937     }
938
939     p_image->i_rows =  i_lines;
940     p_image->i_cols[i_lines] = i_cols_last;
941
942     /* Check word-aligned bits */
943     if( bs_show( s, 8 ) == 0x00 )
944     {
945         bs_skip( s, 8 );
946     }
947
948     return p_image;
949 }
950
951 static void add_rle_code( dvbsub_image_t *p, uint16_t num, uint8_t color,
952                           int i_bpp )
953 {
954     if( p->p_last != NULL )
955     {
956         p->p_last->p_next = malloc( sizeof(dvbsub_rle_t) );
957         p->p_last = p->p_last->p_next;
958     }
959     else
960     {
961         p->p_codes = malloc( sizeof(dvbsub_rle_t) );
962         p->p_last = p->p_codes;
963     }
964     p->p_last->i_num = num;
965
966     p->p_last->i_color_code = color;
967     p->p_last->i_bpp = i_bpp;
968     p->p_last->p_next = NULL;
969 }
970
971 static uint16_t dvbsub_pdata2bpp( bs_t *s, uint16_t* p,
972                                   dvbsub_image_t* p_image )
973 {
974     uint16_t i_processed = 0;
975     vlc_bool_t b_stop = 0;
976     uint16_t i_count = 0;
977     uint8_t i_color = 0;
978
979     while( !b_stop )
980     {
981         i_processed += 2;
982         if( (i_color = bs_read( s, 2 )) != 0x00 )
983         {
984             (*p)++;
985
986             /* Add 1 pixel */
987             add_rle_code( p_image, 1, i_color, 2 );
988         }
989         else
990         {
991             i_processed++;
992             if( bs_read( s, 1 ) == 0x00 )         // Switch1
993             {
994                 i_count = 3 + bs_read( s, 3 );
995                 (*p) += i_count ;
996                 i_color = bs_read( s, 2 );
997                 add_rle_code( p_image, i_count, i_color, 2 );
998                 i_processed += 5;
999             }
1000             else
1001             {
1002                 i_processed++;
1003                 if( bs_read( s, 1 ) == 0x00 )     //Switch2
1004                 {
1005                     i_processed += 2;
1006                     switch( bs_read( s, 2 ) )     //Switch3
1007                     {
1008                     case 0x00:
1009                         b_stop=1;
1010                         break;
1011                     case 0x01:
1012                         add_rle_code( p_image, 2, 0, 2 );
1013                         break;
1014                     case 0x02:
1015                         i_count =  12 + bs_read( s, 4 );
1016                         i_color = bs_read( s, 2 );
1017                         (*p) += i_count;
1018                         i_processed += 6;
1019                         add_rle_code( p_image, i_count, i_color, 2 );
1020                         break;
1021                     case 0x03:
1022                         i_count =  29 + bs_read( s, 8 );
1023                         i_color = bs_read( s, 2 );
1024                         (*p) += i_count;
1025                         i_processed += 10;
1026                         add_rle_code( p_image, i_count, i_color, 2 );
1027                         break;
1028                     default:
1029                         break;
1030                     }
1031                 }
1032             }
1033         }
1034     }
1035
1036     bs_align( s );
1037
1038     return ( i_processed + 7 ) / 8 ;
1039 }
1040
1041 static uint16_t dvbsub_pdata4bpp( bs_t *s, uint16_t* p,
1042                                   dvbsub_image_t* p_image )
1043 {
1044     uint16_t i_processed = 0;
1045     vlc_bool_t b_stop = 0;
1046     uint16_t i_count = 0;
1047     uint8_t i_color = 0;
1048
1049     while( !b_stop )
1050     {
1051         if( (i_color = bs_read( s, 4 )) != 0x00 )
1052         {
1053             (*p)++;
1054             i_processed+=4;
1055
1056             /* Add 1 pixel */
1057             add_rle_code( p_image, 1, i_color, 4 );
1058         }
1059         else
1060         {
1061             if( bs_read( s, 1 ) == 0x00 )           // Switch1
1062             {
1063                 if( bs_show( s, 3 ) != 0x00 )
1064                 {
1065                     i_count = 2 + bs_read( s, 3 );
1066                     (*p) += i_count ;
1067                     add_rle_code( p_image, i_count, 0x00, 4 );
1068                 }
1069                 else
1070                 {
1071                     bs_skip( s, 3 );
1072                     b_stop=1;
1073                 }
1074                 i_processed += 8;
1075             }
1076             else
1077             {
1078                 if( bs_read( s, 1 ) == 0x00)        //Switch2
1079                 {
1080                     i_count =  4 + bs_read( s, 2 );
1081                     i_color = bs_read( s, 4 );
1082                     (*p) += i_count;
1083                     i_processed += 12;
1084                     add_rle_code( p_image, i_count, i_color, 4 );
1085                 }
1086                 else
1087                 {
1088                     switch ( bs_read( s, 2 ) )     //Switch3
1089                     {
1090                         case 0x0:
1091                             (*p)++;
1092                             i_processed += 8;
1093                             add_rle_code( p_image, 1, 0x00, 4 );
1094                             break;
1095                         case 0x1:
1096                             (*p)+=2;
1097                             i_processed += 8;
1098                             add_rle_code( p_image, 2, 0x00, 4 );
1099                             break;
1100                         case 0x2:
1101                              i_count = 9 + bs_read( s, 4 );
1102                              i_color = bs_read( s, 4 );
1103                              (*p)+= i_count;
1104                              i_processed += 16;
1105                              add_rle_code( p_image, i_count, i_color, 4 );
1106                              break;
1107                         case 0x3:
1108                              i_count= 25 + bs_read( s, 8 );
1109                              i_color = bs_read( s, 4 );
1110                              (*p)+= i_count;
1111                              i_processed += 20;
1112                              add_rle_code( p_image, i_count, i_color, 4 );
1113                              break;
1114                     }
1115                 }
1116             }
1117         }
1118     }
1119
1120     bs_align( s );
1121
1122     return ( i_processed + 7 ) / 8 ;
1123 }
1124
1125 static uint16_t dvbsub_pdata8bpp( bs_t *s, uint16_t* p,
1126                                   dvbsub_image_t* p_image )
1127 {
1128     uint16_t i_processed = 0;
1129     vlc_bool_t b_stop = 0;
1130     uint16_t i_count = 0;
1131     uint8_t i_color = 0;
1132
1133     while( !b_stop )
1134     {
1135         i_processed += 8;
1136         if( (i_color = bs_read( s, 8 )) != 0x00 )
1137         {
1138             (*p)++;
1139
1140             /* Add 1 pixel */
1141             add_rle_code( p_image, 1, i_color, 8 );
1142         }
1143         else
1144         {
1145             i_processed++;
1146             if( bs_read( s, 1 ) == 0x00 )           // Switch1
1147             {
1148                 if( bs_show( s, 7 ) != 0x00 )
1149                 {
1150                     i_count = bs_read( s, 7 );
1151                     (*p) += i_count ;
1152                     add_rle_code( p_image, i_count, 0x00, 8 );
1153                 }
1154                 else
1155                 {
1156                     bs_skip( s, 7 );
1157                     b_stop = 1;
1158                 }
1159                 i_processed += 7;
1160             }
1161             else
1162             {
1163                 i_count = bs_read( s, 7 );
1164                 (*p) += i_count ;
1165                 i_color = bs_read( s, 8 );
1166                 add_rle_code( p_image, i_count, i_color, 8 );
1167                 i_processed += 15;
1168             }
1169         }
1170     }
1171
1172     bs_align( s );
1173
1174     return ( i_processed + 7 ) / 8 ;
1175 }
1176
1177 static dvbsub_image_t *dup_image( dvbsub_image_t *p_i )
1178 {
1179     dvbsub_image_t *p_image = malloc( sizeof(dvbsub_image_t) );
1180     dvbsub_rle_t *p_rle = p_i->p_codes;
1181
1182     *p_image = *p_i;
1183     p_image->p_last = NULL;
1184
1185     while( p_rle )
1186     {
1187         dvbsub_rle_t *p_last = p_image->p_last;
1188         p_image->p_last = malloc( sizeof(dvbsub_rle_t) );
1189         if( !p_last ) p_image->p_codes = p_image->p_last;
1190         if( p_last ) p_last->p_next = p_image->p_last;
1191         *p_image->p_last = *p_rle;
1192         p_rle = p_rle->p_next;
1193     }
1194
1195     return p_image;
1196 }
1197
1198 static void free_image( dvbsub_image_t *p_i )
1199 {
1200     dvbsub_rle_t *p1;
1201     dvbsub_rle_t *p2 = NULL;
1202
1203     for( p1 = p_i->p_codes; p1 != NULL; p1 = p2 )
1204     {
1205         p2 = p1->p_next;
1206         free( p1 );
1207         p1 = NULL;
1208     }
1209
1210     free( p_i );
1211 }
1212
1213 static void free_spu( subpicture_t *p_spu )
1214 {
1215     dvbsub_render_t *p_obj, *p_obj_next;
1216
1217     for( p_obj = p_spu->p_sys->p_objects; p_obj != NULL; p_obj = p_obj_next )
1218     {
1219         p_obj_next = p_obj->p_next;
1220         free_image( p_obj->p_rle_top );
1221         free_image( p_obj->p_rle_bot );
1222         free( p_obj );
1223     }
1224     free( p_spu->p_sys );
1225     p_spu->p_sys = NULL;
1226 }
1227
1228 static void free_objects( decoder_t *p_dec )
1229 {
1230     decoder_sys_t *p_sys = p_dec->p_sys;
1231     dvbsub_object_t *p_obj, *p_obj_next;
1232
1233     for( p_obj = p_sys->p_objects; p_obj != NULL; p_obj = p_obj_next )
1234     {
1235         p_obj_next = p_obj->p_next;
1236         free_image( p_obj->topfield );
1237         free_image( p_obj->bottomfield );
1238         free( p_obj );
1239     }
1240     p_sys->p_objects = NULL;
1241 }
1242
1243 static void free_all( decoder_t *p_dec )
1244 {
1245     decoder_sys_t *p_sys = p_dec->p_sys;
1246     dvbsub_region_t *p_reg, *p_reg_next;
1247     int i;
1248
1249     for( i = 0; i < 256; i++ )
1250     {
1251         if( p_sys->p_clut[i] ) free( p_sys->p_clut[i] );
1252         p_sys->p_clut[i] = NULL;
1253     }
1254
1255     if( p_sys->p_page )
1256     {
1257         if( p_sys->p_page->i_region_defs )
1258             free( p_sys->p_page->p_region_defs );
1259         free( p_sys->p_page );
1260         p_sys->p_page = NULL;
1261     }
1262
1263     for( p_reg = p_sys->p_regions; p_reg != NULL; p_reg = p_reg_next )
1264     {
1265         p_reg_next = p_reg->p_next;
1266         if( p_reg->i_object_defs ) free( p_reg->p_object_defs );
1267         free( p_reg );
1268     }
1269     p_sys->p_regions = NULL;
1270
1271     free_objects( p_dec );
1272 }
1273
1274 static void RenderYUY2( vout_thread_t *p_vout, picture_t *p_pic,
1275                         dvbsub_render_t *p_r )
1276 {
1277     /* Common variables */
1278     uint8_t  *p_desty;
1279     uint16_t i,j;
1280     uint16_t i_cnt;
1281     uint16_t x, y;
1282     dvbsub_rle_t* p_c;
1283     dvbsub_image_t* p_im = p_r->p_rle_top;
1284     i=0;
1285     j=0;
1286     p_desty = p_pic->Y_PIXELS;
1287     //let's render the 1st frame
1288     for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1289     {
1290         //if( p_c->y != 0  && p_c->t < 0x20)
1291         if( p_c->y != 0  && p_c->t < 0x20)
1292         {
1293             x = j+ p_r->i_x;
1294             y = 2*i+p_r->i_y;
1295             //memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1296             // In YUY2 we have to set pixel per pixel
1297             for( i_cnt = 0; i_cnt < p_c->i_num; i_cnt+=2 )
1298             {
1299                 memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt, p_c->y, 1);
1300               //memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+1, p_c->cr, 1);
1301               //memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+2, p_c->y, 1);
1302               //memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+3, p_c->cb, 1);
1303             }
1304         }
1305         j += p_c->i_num;
1306         if(j >= p_im->i_cols[i])
1307         {
1308             i++; j=0;
1309         }
1310         if( i>= p_im->i_rows) break;
1311     }
1312     //idem for the second frame
1313     p_im = p_r->p_rle_bot; i=0; j=0;
1314     for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1315     {
1316         if( p_c->y != 0 && p_c->t < 0x20)
1317         {
1318             x = j+ p_r->i_x;
1319             y = 2*i+1+p_r->i_y;
1320             //memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1321             // In YUY2 we have to set pixel per pixel
1322             for( i_cnt = 0; i_cnt < p_c->i_num; i_cnt+=2 )
1323             {
1324                 memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt, p_c->y, 1);
1325               //memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+1, p_c->cr, 1);
1326               //memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+2, p_c->y, 1);
1327               //memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+3, p_c->cb, 1);
1328            }
1329         }
1330         j += p_c->i_num;
1331         if(j >= p_im->i_cols[i])
1332         {
1333             i++; j=0;
1334         }
1335         if( i>= p_im->i_rows) break;
1336     }
1337 }
1338
1339 static void RenderI42x( vout_thread_t *p_vout, picture_t *p_pic,
1340                         dvbsub_render_t *p_r )
1341 {
1342     /* Common variables */
1343     uint8_t *p_desty = p_pic->Y_PIXELS;
1344     uint8_t *p_destu = p_pic->U_PIXELS;
1345     uint8_t *p_destv = p_pic->V_PIXELS;
1346     dvbsub_image_t* p_im = p_r->p_rle_top;
1347     dvbsub_rle_t* p_c;
1348     uint16_t i, j, x, y;
1349     int i_x_subsampling =
1350         p_vout->output.i_chroma == VLC_FOURCC('I','4','2','2') ? 1 : 2;
1351
1352     /* Let's render the top field */
1353     p_im = p_r->p_rle_bot; i = 0; j = 0;
1354     for( p_c = p_im->p_codes; p_c->p_next != NULL; p_c = p_c->p_next )
1355     {
1356         if( p_c->y != 0 && p_c->t != 0xFF )
1357         {
1358             x = j + p_r->i_x;
1359             y = 2 * i + p_r->i_y;
1360             memset( p_desty + y * p_pic->Y_PITCH + x, p_c->y, p_c->i_num );
1361
1362             memset( p_destu + y/2 * p_pic->U_PITCH + x/i_x_subsampling,
1363                     p_c->cr, p_c->i_num/i_x_subsampling );
1364             memset( p_destv + y/2 * p_pic->V_PITCH + x/i_x_subsampling,
1365                     p_c->cb, p_c->i_num/i_x_subsampling );
1366         }
1367
1368         j += p_c->i_num;
1369         if( j >= p_im->i_cols[i] )
1370         {
1371             i++; j=0;
1372         }
1373
1374         if( i >= p_im->i_rows) break;
1375     }
1376
1377     /* Idem for the bottom field */
1378     p_im = p_r->p_rle_bot; i = 0; j = 0;
1379     for( p_c = p_im->p_codes; p_c->p_next != NULL; p_c = p_c->p_next )
1380     {
1381       if( p_c->y != 0  && p_c->t != 0xFF )
1382         {
1383             x = j + p_r->i_x;
1384             y = 2*i + 1 + p_r->i_y;
1385
1386             memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1387
1388             /* No U or V (decimation) */
1389         }
1390
1391         j += p_c->i_num;
1392         if( j >= p_im->i_cols[i] )
1393         {
1394             i++; j=0;
1395         }
1396
1397         if( i >= p_im->i_rows ) break;
1398     }
1399 }
1400
1401 static void RenderDVBSUB( vout_thread_t *p_vout, picture_t *p_pic,
1402                           const subpicture_t *p_spu )
1403 {
1404     dvbsub_render_t* p_render;
1405
1406     if( p_spu->p_sys == NULL ) return;
1407
1408     p_render = p_spu->p_sys->p_objects;
1409     while( p_render )
1410     {
1411
1412         switch( p_vout->output.i_chroma )
1413         {
1414         /* I420 target, no scaling */
1415         case VLC_FOURCC('I','4','2','2'):
1416         case VLC_FOURCC('I','4','2','0'):
1417         case VLC_FOURCC('I','Y','U','V'):
1418         case VLC_FOURCC('Y','V','1','2'):
1419             /* As long as we just use Y info, I422 and YV12 are just equivalent
1420              * to I420. Remember to change it the day we'll take into account
1421              * U and V info. */
1422             RenderI42x( p_vout, p_pic, p_render );
1423             break;
1424
1425         /* RV16 target, scaling */
1426         case VLC_FOURCC('R','V','1','6'):
1427             msg_Err(p_vout, "unimplemented chroma: RV16");
1428             /* RenderRV16( p_vout, p_pic, p_spu ); */
1429             break;
1430
1431         /* RV32 target, scaling */
1432         case VLC_FOURCC('R','V','2','4'):
1433         case VLC_FOURCC('R','V','3','2'):
1434             msg_Err(p_vout, "unimplemented chroma: RV32");
1435             /* RenderRV32( p_vout, p_pic, p_spu ); */
1436             break;
1437
1438         /* NVidia overlay, no scaling */
1439         case VLC_FOURCC('Y','U','Y','2'):
1440             RenderYUY2( p_vout, p_pic, p_render );
1441             break;
1442
1443         default:
1444             msg_Err( p_vout, "unknown chroma, can't render SPU" );
1445             break;
1446         }
1447
1448         p_render = p_render->p_next;
1449     }
1450 }
1451
1452 static void render( decoder_t *p_dec, vout_thread_t *p_vout )
1453 {
1454     decoder_sys_t   *p_sys = p_dec->p_sys;
1455     dvbsub_render_t *p_render = NULL, *p_current, *p_last = NULL;
1456     dvbsub_clut_t   *p_clut;
1457     dvbsub_rle_t    *p_c;
1458     int i, j = 0, i_timeout = 0;
1459
1460     /* Allocate the subpicture internal data. */
1461 #ifdef DEBUG_DVBSUB
1462     if( !p_vout ) p_sys->p_spu = malloc( sizeof(subpicture_t) );
1463     else
1464 #endif
1465     p_sys->p_spu =
1466         vout_CreateSubPicture( p_vout, p_sys->i_subpic_channel,
1467                                MEMORY_SUBPICTURE );
1468     if( p_sys->p_spu == NULL ) return;
1469
1470     /* Loop on region definitions */
1471 #ifdef DEBUG_DVBSUB
1472     if( p_sys->p_page )
1473         msg_Dbg( p_dec, "rendering %i regions", p_sys->p_page->i_region_defs );
1474 #endif
1475
1476     for( i = 0; p_sys->p_page && i < p_sys->p_page->i_region_defs; i++ )
1477     {
1478         dvbsub_region_t    *p_region;
1479         dvbsub_regiondef_t *p_regiondef;
1480
1481         i_timeout = p_sys->p_page->i_timeout;
1482
1483         p_regiondef = &p_sys->p_page->p_region_defs[i];
1484
1485 #ifdef DEBUG_DVBSUB
1486         msg_Dbg( p_dec, "rendering region %i (%i,%i)", i,
1487                  p_regiondef->i_x, p_regiondef->i_y );
1488 #endif
1489
1490         /* Find associated region */
1491         for( p_region = p_sys->p_regions; p_region != NULL;
1492              p_region = p_region->p_next )
1493         {
1494             if( p_regiondef->i_id == p_region->i_id ) break;
1495         }
1496
1497         if( !p_region )
1498         {
1499             msg_Err( p_dec, "no region founddddd!!!" );
1500             continue;
1501         }
1502
1503         /* Loop on object definitions */
1504         for( j = 0; j < p_region->i_object_defs; j++ )
1505         {
1506             dvbsub_object_t    *p_object;
1507             dvbsub_objectdef_t *p_objectdef;
1508
1509             p_objectdef = &p_region->p_object_defs[j];
1510
1511 #ifdef DEBUG_DVBSUB
1512             msg_Dbg( p_dec, "rendering object %i (%i,%i)", p_objectdef->i_id,
1513                      p_objectdef->i_x, p_objectdef->i_y );
1514 #endif
1515
1516             /* Look for the right object */
1517             for( p_object = p_sys->p_objects; p_object != NULL;
1518                  p_object = p_object->p_next )
1519             {
1520                 if( p_objectdef->i_id == p_object->i_id ) break;
1521             }
1522
1523             if( !p_object )
1524             {
1525                 msg_Err( p_dec, "no object founddddd!!!" );
1526                 continue;
1527             }
1528
1529             /* Allocate the render structure */
1530             p_current = malloc( sizeof(dvbsub_render_t) );
1531             p_current->p_next = NULL;
1532             p_current->i_x = p_regiondef->i_x + p_objectdef->i_x;
1533             p_current->i_y = p_regiondef->i_y + p_objectdef->i_y;
1534             p_current->p_rle_top = dup_image( p_object->topfield );
1535             p_current->p_rle_bot = dup_image( p_object->bottomfield );
1536
1537             if( !p_render ) p_render = p_current;
1538             if( p_last ) p_last->p_next = p_current;
1539             p_last = p_current;
1540
1541             p_clut = p_sys->p_clut[p_region->i_clut];
1542             if( !p_clut ) p_clut = &p_sys->default_clut;
1543
1544             /* Compute the color datas according to the appropriate CLUT */
1545             for( p_c = p_current->p_rle_top->p_codes;
1546                  p_c->p_next != NULL; p_c = p_c->p_next )
1547             {
1548                 dvbsub_color_t *p_color = (p_c->i_bpp == 2) ? p_clut->c_2b :
1549                     (p_c->i_bpp == 4) ? p_clut->c_4b : p_clut->c_8b;
1550
1551                 p_c->y = p_color[p_c->i_color_code].Y;
1552                 p_c->cr = p_color[p_c->i_color_code].Cr;
1553                 p_c->cb = p_color[p_c->i_color_code].Cb;
1554                 p_c->t = p_color[p_c->i_color_code].T;
1555             }
1556             for( p_c = p_current->p_rle_bot->p_codes; p_c->p_next != NULL;
1557                  p_c = p_c->p_next )
1558             {
1559                 dvbsub_color_t *p_color = (p_c->i_bpp == 2) ? p_clut->c_2b :
1560                     (p_c->i_bpp == 4) ? p_clut->c_4b : p_clut->c_8b;
1561
1562                 p_c->y = p_color[p_c->i_color_code].Y;
1563                 p_c->cr = p_color[p_c->i_color_code].Cr;
1564                 p_c->cb = p_color[p_c->i_color_code].Cb;
1565                 p_c->t = p_color[p_c->i_color_code].T;
1566             }
1567         }
1568     }
1569
1570     /* Set the pf_render callback */
1571     p_sys->p_spu->pf_render = RenderDVBSUB;
1572     p_sys->p_spu->p_sys = malloc( sizeof(subpicture_sys_t) );
1573     p_sys->p_spu->p_sys->p_objects = p_render;
1574     p_sys->p_spu->pf_destroy = free_spu;
1575     p_sys->p_spu->i_start = p_sys->i_pts;
1576     p_sys->p_spu->i_stop = p_sys->p_spu->i_start + i_timeout * 1000000;
1577     p_sys->p_spu->b_ephemer = VLC_TRUE;
1578
1579 #ifdef DEBUG_DVBSUB
1580     if( !p_vout )
1581     {
1582         free_spu( p_sys->p_spu );
1583         free( p_sys->p_spu );
1584         p_sys->p_spu = NULL;
1585         return;
1586     }
1587 #endif
1588
1589     vout_DisplaySubPicture( p_vout, p_sys->p_spu );
1590 }