]> git.sesse.net Git - vlc/blob - modules/codec/dvbsub.c
* modules/codec/dvbsub.c:
[vlc] / modules / codec / dvbsub.c
1 /*****************************************************************************
2  * dvbsub.c : DVB subtitles decoder
3  *            DVB subtitles encoder (developed for Anevia, www.anevia.com)
4  *****************************************************************************
5  * Copyright (C) 2003 ANEVIA
6  * Copyright (C) 2003-2004 VideoLAN
7  * $Id$
8  *
9  * Authors: Damien LUCAS <damien.lucas@anevia.com>
10  *          Laurent Aimar <fenrir@via.ecp.fr>
11  *          Gildas Bazin <gbazin@videolan.org>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
26  *****************************************************************************/
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32 #include <vlc/decoder.h>
33 #include <vlc/sout.h>
34
35 #include "vlc_bits.h"
36
37 //#define DEBUG_DVBSUB 1
38
39 /*****************************************************************************
40  * Module descriptor.
41  *****************************************************************************/
42 static int  Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
44 static subpicture_t *Decode( decoder_t *, block_t ** );
45
46 static int OpenEncoder  ( vlc_object_t * );
47 static void CloseEncoder( vlc_object_t * );
48 static block_t *Encode  ( encoder_t *, subpicture_t * );
49
50 vlc_module_begin();
51     set_description( _("DVB subtitles decoder") );
52     set_capability( "decoder", 50 );
53     set_callbacks( Open, Close );
54
55 #   define ENC_CFG_PREFIX "sout-dvbsub-"
56     add_submodule();
57     set_description( _("DVB subtitles encoder") );
58     set_capability( "encoder", 100 );
59     set_callbacks( OpenEncoder, CloseEncoder );
60 vlc_module_end();
61
62 static const char *ppsz_enc_options[] = { NULL };
63
64 /****************************************************************************
65  * Local structures
66  ****************************************************************************
67  * Those structures refer closely to the ETSI 300 743 Object model
68  ****************************************************************************/
69
70 /* Storage of a RLE entry */
71 typedef struct dvbsub_rle_s
72 {
73     uint16_t                i_num;
74     int                     i_color_code;
75     int                     i_bpp;
76     uint8_t                 y;
77     uint8_t                 cr;
78     uint8_t                 cb;
79     uint8_t                 t;
80     struct dvbsub_rle_s     *p_next;
81
82 } dvbsub_rle_t;
83
84 /* A subpicture image is a list of codes
85  * We need to store the length of each line since nothing specify in
86  * the standard that all lines should have the same length
87  * WARNING: We assume here that a spu is less than 576 lines high */
88 typedef struct
89 {
90     uint16_t                i_rows;
91     uint16_t                i_cols[576];
92     dvbsub_rle_t            *p_last;
93     dvbsub_rle_t            *p_codes;
94
95 } dvbsub_image_t;
96
97 /* The object definition gives the position of the object in a region */
98 typedef struct dvbsub_objectdef_s
99 {
100     uint16_t                  i_id;
101     uint8_t                   i_type;
102     uint8_t                   i_provider;
103     uint16_t                  i_x;
104     uint16_t                  i_y;
105     uint8_t                   i_fg_pc;
106     uint8_t                   i_bg_pc;
107
108 } dvbsub_objectdef_t;
109
110 /* An object is constituted of 2 images (for interleaving) */
111 typedef struct dvbsub_object_s
112 {
113     uint16_t                i_id;
114     uint8_t                 i_version_number;
115     uint8_t                 i_coding_method;
116     vlc_bool_t              b_non_modify_color;
117     dvbsub_image_t         *topfield;
118     dvbsub_image_t         *bottomfield;
119     struct dvbsub_object_s *p_next;
120
121 } dvbsub_object_t;
122
123 /* The object definition gives the position of the object in a region */
124 typedef struct dvbsub_regiondef_s
125 {
126     uint16_t                  i_id;
127     uint16_t                  i_x;
128     uint16_t                  i_y;
129
130 } dvbsub_regiondef_t;
131
132 /* The Region is an aera on the image
133  * with a list of the object definitions associated and a CLUT */
134 typedef struct dvbsub_region_s
135 {
136     uint8_t                 i_id;
137     uint8_t                 i_version_number;
138     vlc_bool_t              b_fill;
139     uint16_t                i_x;
140     uint16_t                i_y;
141     uint16_t                i_width;
142     uint16_t                i_height;
143     uint8_t                 i_level_comp;
144     uint8_t                 i_depth;
145     uint8_t                 i_clut;
146     uint8_t                 i_8bp_code;
147     uint8_t                 i_4bp_code;
148     uint8_t                 i_2bp_code;
149
150     int                     i_object_defs;
151     dvbsub_objectdef_t      *p_object_defs;
152
153     struct dvbsub_region_s  *p_next;
154
155 } dvbsub_region_t;
156
157 /* The page defines the list of regions */
158 typedef struct
159 {
160     uint16_t              i_id;
161     uint8_t               i_timeout;
162     uint8_t               i_state;
163     uint8_t               i_version_number;
164
165     uint8_t               i_region_defs;
166     dvbsub_regiondef_t    *p_region_defs;
167
168 } dvbsub_page_t;
169
170 /* The entry in the palette CLUT */
171 typedef struct
172 {
173     uint8_t                 Y;
174     uint8_t                 Cr;
175     uint8_t                 Cb;
176     uint8_t                 T;
177
178 } dvbsub_color_t;
179
180 /* */
181 typedef struct
182 {
183     uint8_t                 i_id;
184     uint8_t                 i_version_number;
185     dvbsub_color_t          c_2b[4];
186     dvbsub_color_t          c_4b[16];
187     dvbsub_color_t          c_8b[256];
188
189 } dvbsub_clut_t;
190
191 struct decoder_sys_t
192 {
193     bs_t            bs;
194
195     /* Decoder internal data */
196     int             i_id;
197     int             i_ancillary_id;
198     mtime_t         i_pts;
199
200     dvbsub_page_t   *p_page;
201     dvbsub_region_t *p_regions;
202     dvbsub_object_t *p_objects;
203
204     dvbsub_clut_t   *p_clut[256];
205     dvbsub_clut_t   default_clut;
206 };
207
208
209 // List of different SEGMENT TYPES
210 // According to EN 300-743, table 2
211 #define DVBSUB_ST_PAGE_COMPOSITION      0x10
212 #define DVBSUB_ST_REGION_COMPOSITION    0x11
213 #define DVBSUB_ST_CLUT_DEFINITION       0x12
214 #define DVBSUB_ST_OBJECT_DATA           0x13
215 #define DVBSUB_ST_ENDOFDISPLAY          0x80
216 #define DVBSUB_ST_STUFFING              0xff
217 // List of different OBJECT TYPES
218 // According to EN 300-743, table 6
219 #define DVBSUB_OT_BASIC_BITMAP          0x00
220 #define DVBSUB_OT_BASIC_CHAR            0x01
221 #define DVBSUB_OT_COMPOSITE_STRING      0x02
222 // Pixel DATA TYPES
223 // According to EN 300-743, table 9
224 #define DVBSUB_DT_2BP_CODE_STRING       0x10
225 #define DVBSUB_DT_4BP_CODE_STRING       0x11
226 #define DVBSUB_DT_8BP_CODE_STRING       0x12
227 #define DVBSUB_DT_24_TABLE_DATA         0x20
228 #define DVBSUB_DT_28_TABLE_DATA         0x21
229 #define DVBSUB_DT_48_TABLE_DATA         0x22
230 #define DVBSUB_DT_END_LINE              0xf0
231 // List of different Page Composition Segment state
232 // According to EN 300-743, 7.2.1 table 3
233 #define DVBSUB_PCS_STATE_ACQUISITION    0x01
234 #define DVBSUB_PCS_STATE_CHANGE         0x10
235
236 /*****************************************************************************
237  * Local prototypes
238  *****************************************************************************/
239 static void decode_segment( decoder_t *, bs_t * );
240 static void decode_page_composition( decoder_t *, bs_t * );
241 static void decode_region_composition( decoder_t *, bs_t * );
242 static void decode_object( decoder_t *, bs_t * );
243 static void decode_clut( decoder_t *, bs_t * );
244
245 static void free_objects( decoder_t * );
246 static void free_all( decoder_t * );
247
248 static subpicture_t *render( decoder_t * );
249
250 static void default_clut_init( decoder_t * );
251
252 /*****************************************************************************
253  * Open: probe the decoder and return score
254  *****************************************************************************
255  * Tries to launch a decoder and return score so that the interface is able
256  * to chose.
257  *****************************************************************************/
258 static int Open( vlc_object_t *p_this )
259 {
260     decoder_t     *p_dec = (decoder_t *) p_this;
261     decoder_sys_t *p_sys;
262     int i;
263
264     if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','v','b','s') )
265     {
266         return VLC_EGENERIC;
267     }
268
269     p_dec->pf_decode_sub = Decode;
270     p_sys = p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
271
272     p_sys->i_pts          = 0;
273     p_sys->i_id           = p_dec->fmt_in.subs.dvb.i_id & 0xFFFF;
274     p_sys->i_ancillary_id = p_dec->fmt_in.subs.dvb.i_id >> 16;
275     p_sys->p_page         = NULL;
276     p_sys->p_regions      = NULL;
277     p_sys->p_objects      = NULL;
278     for( i = 0; i < 256; i++ ) p_sys->p_clut[i] = NULL;
279
280     es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'd','v','b','s' ) );
281
282     default_clut_init( p_dec );
283
284     return VLC_SUCCESS;
285 }
286
287 /*****************************************************************************
288  * Close:
289  *****************************************************************************/
290 static void Close( vlc_object_t *p_this )
291 {
292     decoder_t     *p_dec = (decoder_t*) p_this;
293     decoder_sys_t *p_sys = p_dec->p_sys;
294
295     free_all( p_dec );
296     free( p_sys );
297 }
298
299 /*****************************************************************************
300  * Decode:
301  *****************************************************************************/
302 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
303 {
304     decoder_sys_t *p_sys = p_dec->p_sys;
305     block_t       *p_block;
306     subpicture_t  *p_spu = NULL;
307
308     if( pp_block == NULL || *pp_block == NULL ) return NULL;
309     p_block = *pp_block;
310     *pp_block = NULL;
311
312     p_sys->i_pts = p_block->i_pts;
313     if( p_sys->i_pts <= 0 )
314     {
315 #ifdef DEBUG_DVBSUB
316         /* Some DVB channels send stuffing segments in non-dated packets so
317          * don't complain too loudly. */
318         msg_Warn( p_dec, "non dated subtitle" );
319 #endif
320         block_Release( p_block );
321         return NULL;
322     }
323
324     bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
325
326     if( bs_read( &p_sys->bs, 8 ) != 0x20 ) /* Data identifier */
327     {
328         msg_Dbg( p_dec, "invalid data identifier" );
329         block_Release( p_block );
330         return NULL;
331     }
332
333     if( bs_read( &p_sys->bs, 8 ) ) /* Subtitle stream id */
334     {
335         msg_Dbg( p_dec, "invalid subtitle stream id" );
336         block_Release( p_block );
337         return NULL;
338     }
339
340 #ifdef DEBUG_DVBSUB
341     msg_Dbg( p_dec, "subtitle packet received: "I64Fd, p_sys->i_pts );
342 #endif
343
344     while( bs_show( &p_sys->bs, 8 ) == 0x0f ) /* Sync byte */
345     {
346         decode_segment( p_dec, &p_sys->bs );
347     }
348
349     if( bs_read( &p_sys->bs, 8 ) != 0xff ) /* End marker */
350     {
351         msg_Warn( p_dec, "end marker not found (corrupted subtitle ?)" );
352         block_Release( p_block );
353         return NULL;
354     }
355
356     /* Check if the page is to be displayed */
357     if( p_sys->p_page ) p_spu = render( p_dec );
358
359     block_Release( p_block );
360
361     return p_spu;
362 }
363
364 /* following functions are local */
365
366 /*****************************************************************************
367  * default_clut_init: default clut as defined in EN 300-743 section 10
368  *****************************************************************************/
369 static void default_clut_init( decoder_t *p_dec )
370 {
371     decoder_sys_t *p_sys = p_dec->p_sys;
372     uint8_t i;
373
374 #define RGB_TO_Y(r, g, b) ((int16_t) 77 * r + 150 * g + 29 * b) / 256;
375 #define RGB_TO_U(r, g, b) ((int16_t) -44 * r - 87 * g + 131 * b) / 256;
376 #define RGB_TO_V(r, g, b) ((int16_t) 131 * r - 110 * g - 21 * b) / 256;
377
378     /* 4 entries CLUT */
379     for( i = 0; i < 4; i++ )
380     {
381         uint8_t R = 0, G = 0, B = 0, T = 0;
382
383         if( !(i & 0x2) && !(i & 0x1) ) T = 0xFF;
384         else if( !(i & 0x2) && (i & 0x1) ) R = G = B = 0xFF;
385         else if( (i & 0x2) && !(i & 0x1) ) R = G = B = 0;
386         else R = G = B = 0x7F;
387
388         p_sys->default_clut.c_2b[i].Y = RGB_TO_Y(R,G,B);
389         p_sys->default_clut.c_2b[i].Cr = RGB_TO_U(R,G,B);
390         p_sys->default_clut.c_2b[i].Cb = RGB_TO_V(R,G,B);
391         p_sys->default_clut.c_2b[i].T = T;
392     }
393
394     /* 16 entries CLUT */
395     for( i = 0; i < 16; i++ )
396     {
397         uint8_t R = 0, G = 0, B = 0, T = 0;
398
399         if( !(i & 0x8) )
400         {
401             if( !(i & 0x4) && !(i & 0x2) && !(i & 0x1) )
402             {
403                 T = 0xFF;
404             }
405             else
406             {
407                 R = (i & 0x1) ? 0xFF : 0;
408                 G = (i & 0x2) ? 0xFF : 0;
409                 B = (i & 0x4) ? 0xFF : 0;
410             }
411         }
412         else
413         {
414             R = (i & 0x1) ? 0x7F : 0;
415             G = (i & 0x2) ? 0x7F : 0;
416             B = (i & 0x4) ? 0x7F : 0;
417         }
418
419         p_sys->default_clut.c_4b[i].Y = RGB_TO_Y(R,G,B);
420         p_sys->default_clut.c_4b[i].Cr = RGB_TO_U(R,G,B);
421         p_sys->default_clut.c_4b[i].Cb = RGB_TO_V(R,G,B);
422         p_sys->default_clut.c_4b[i].T = T;
423     }
424
425     /* 256 entries CLUT (TODO) */
426     memset( p_sys->default_clut.c_8b, 0xFF, 256 * sizeof(dvbsub_color_t) );
427 }
428
429 static void decode_segment( decoder_t *p_dec, bs_t *s )
430 {
431     decoder_sys_t *p_sys = p_dec->p_sys;
432     int i_type;
433     int i_page_id;
434     int i_size;
435
436     /* sync_byte (already checked) */
437     bs_skip( s, 8 );
438
439     /* segment type */
440     i_type = bs_read( s, 8 );
441
442     /* page id */
443     i_page_id = bs_read( s, 16 );
444
445     /* segment size */
446     i_size = bs_show( s, 16 );
447
448     if( i_page_id != p_sys->i_id && i_page_id != p_sys->i_ancillary_id )
449     {
450 #ifdef DEBUG_DVBSUB
451         msg_Dbg( p_dec, "subtitle skipped (page id: %i, %i)",
452                  i_page_id, p_sys->i_id );
453 #endif
454         bs_skip( s,  8 * ( 2 + i_size ) );
455         return;
456     }
457
458 #ifdef DEBUG_DVBSUB
459     if( i_page_id == p_sys->i_id )
460         msg_Dbg( p_dec, "segment (id: %i)", i_page_id );
461     else
462         msg_Dbg( p_dec, "ancillary segment (id: %i)", i_page_id );
463 #endif
464
465     switch( i_type )
466     {
467     case DVBSUB_ST_PAGE_COMPOSITION:
468 #ifdef DEBUG_DVBSUB
469         msg_Dbg( p_dec, "decode_page_composition" );
470 #endif
471         decode_page_composition( p_dec, s );
472         break;
473
474     case DVBSUB_ST_REGION_COMPOSITION:
475 #ifdef DEBUG_DVBSUB
476         msg_Dbg( p_dec, "decode_region_composition" );
477 #endif
478         decode_region_composition( p_dec, s );
479         break;
480
481     case DVBSUB_ST_CLUT_DEFINITION:
482 #ifdef DEBUG_DVBSUB
483         msg_Dbg( p_dec, "decode_clut" );
484 #endif
485         decode_clut( p_dec, s );
486         break;
487
488     case DVBSUB_ST_OBJECT_DATA:
489 #ifdef DEBUG_DVBSUB
490         msg_Dbg( p_dec, "decode_object" );
491 #endif
492         decode_object( p_dec, s );
493         break;
494
495     case DVBSUB_ST_ENDOFDISPLAY:
496 #ifdef DEBUG_DVBSUB
497         msg_Dbg( p_dec, "end of display" );
498 #endif
499         bs_skip( s,  8 * ( 2 + i_size ) );
500         break;
501
502     case DVBSUB_ST_STUFFING:
503 #ifdef DEBUG_DVBSUB
504         msg_Dbg( p_dec, "skip stuffing" );
505 #endif
506         bs_skip( s,  8 * ( 2 + i_size ) );
507         break;
508
509     default:
510         msg_Warn( p_dec, "unsupported segment type: (%04x)", i_type );
511         bs_skip( s,  8 * ( 2 + i_size ) );
512         break;
513     }
514 }
515
516 static void decode_clut( decoder_t *p_dec, bs_t *s )
517 {
518     decoder_sys_t *p_sys = p_dec->p_sys;
519     uint16_t      i_segment_length;
520     uint16_t      i_processed_length;
521     dvbsub_clut_t *p_clut;
522     uint8_t       i_clut_id;
523     uint8_t       i_version_number;
524
525     i_segment_length = bs_read( s, 16 );
526     i_clut_id        = bs_read( s, 8 );
527     i_version_number = bs_read( s, 4 );
528
529     /* Check that this id doesn't not already exist with the same version
530      * number and allocate memory if necessary */
531     if( p_sys->p_clut[i_clut_id] != NULL &&
532         p_sys->p_clut[i_clut_id]->i_version_number == i_version_number )
533     {
534         /* Nothing to do */
535         bs_skip( s,  8 * i_segment_length - 12 );
536         return;
537     }
538
539     if( !p_sys->p_clut[i_clut_id] )
540     {
541         p_sys->p_clut[i_clut_id] = malloc( sizeof(dvbsub_clut_t) );
542     }
543
544     p_clut = p_sys->p_clut[i_clut_id];
545
546     /* We don't have this version of the CLUT: Parse it */
547     p_clut->i_version_number = i_version_number;
548     bs_skip( s, 4 ); /* Reserved bits */
549     i_processed_length = 2;
550     while( i_processed_length < i_segment_length )
551     {
552         uint8_t y, cb, cr, t;
553         uint8_t i_id;
554         uint8_t i_type;
555
556         i_id = bs_read( s, 8 );
557         i_type = bs_read( s, 3 );
558
559         bs_skip( s, 4 );
560
561         if( bs_read( s, 1 ) )
562         {
563             y  = bs_read( s, 8 );
564             cr = bs_read( s, 8 );
565             cb = bs_read( s, 8 );
566             t  = bs_read( s, 8 );
567             i_processed_length += 6;
568         }
569         else
570         {
571             y  = bs_read( s, 6 ) << 2;
572             cr = bs_read( s, 4 ) << 4;
573             cb = bs_read( s, 4 ) << 4;
574             t  = bs_read( s, 2 ) << 6;
575             i_processed_length += 4;
576         }
577
578         /* We are not entirely compliant here as full transparency is indicated
579          * with a luma value of zero, not a transparency value of 0xff
580          * (full transparency would actually be 0xff + 1). */
581
582         if( y == 0 )
583         {
584             cr = cb = 0;
585             t  = 0xff;
586         }
587
588         /* According to EN 300-743 section 7.2.3 note 1, type should
589          * not have more than 1 bit set to one, but some streams don't
590          * respect this note. */
591
592         if( i_type & 0x04)
593         {
594             p_clut->c_2b[i_id].Y = y;
595             p_clut->c_2b[i_id].Cr = cr;
596             p_clut->c_2b[i_id].Cb = cb;
597             p_clut->c_2b[i_id].T = t;
598         }
599         if( i_type & 0x02)
600         {
601             p_clut->c_4b[i_id].Y = y;
602             p_clut->c_4b[i_id].Cr = cr;
603             p_clut->c_4b[i_id].Cb = cb;
604             p_clut->c_4b[i_id].T = t;
605         }
606         if( i_type & 0x01)
607         {
608             p_clut->c_8b[i_id].Y = y;
609             p_clut->c_8b[i_id].Cr = cr;
610             p_clut->c_8b[i_id].Cb = cb;
611             p_clut->c_8b[i_id].T = t;
612         }
613     }
614 }
615
616 static void decode_page_composition( decoder_t *p_dec, bs_t *s )
617 {
618     decoder_sys_t *p_sys = p_dec->p_sys;
619     unsigned int i_version_number;
620     unsigned int i_state;
621     unsigned int i_segment_length;
622     uint8_t i_timeout;
623     unsigned int i;
624
625     /* A page is composed by one or more region */
626
627     i_segment_length = bs_read( s, 16 );
628     i_timeout = bs_read( s, 8 );
629     i_version_number = bs_read( s, 4 );
630     i_state = bs_read( s, 2 );
631     bs_skip( s, 2 ); /* Reserved */
632
633     if( i_state == DVBSUB_PCS_STATE_CHANGE )
634     {
635         /* End of an epoch, reset decoder buffer */
636 #ifdef DEBUG_DVBSUB
637         msg_Dbg( p_dec, "page composition mode change" );
638 #endif
639         free_all( p_dec );
640     }
641     else if( !p_sys->p_page && i_state != DVBSUB_PCS_STATE_ACQUISITION )
642     {
643         /* Not a full PCS, we need to wait for one */
644         bs_skip( s,  8 * (i_segment_length - 2) );
645         return;
646     }
647
648     if( i_state == DVBSUB_PCS_STATE_ACQUISITION )
649     {
650 #ifdef DEBUG_DVBSUB
651         msg_Dbg( p_dec, "acquisition page composition" );
652 #endif
653         /* Make sure we clean up regularly our objects list.
654          * Is it the best place to do this ? */
655         free_objects( p_dec );
656     }
657
658     /* Check version number */
659     if( p_sys->p_page &&
660         p_sys->p_page->i_version_number == i_version_number )
661     {
662         bs_skip( s,  8 * (i_segment_length - 2) );
663         return;
664     }
665     else if( p_sys->p_page )
666     {
667         if( p_sys->p_page->i_region_defs )
668             free( p_sys->p_page->p_region_defs );
669         p_sys->p_page->i_region_defs = 0;
670     }
671
672     if( !p_sys->p_page )
673     {
674         /* Allocate a new page */
675         p_sys->p_page = malloc( sizeof(dvbsub_page_t) );
676     }
677
678     p_sys->p_page->i_version_number = i_version_number;
679     p_sys->p_page->i_timeout = i_timeout;
680
681     /* Number of regions */
682     p_sys->p_page->i_region_defs = (i_segment_length - 2) / 6;
683
684     if( p_sys->p_page->i_region_defs == 0 ) return;
685
686     p_sys->p_page->p_region_defs =
687         malloc( p_sys->p_page->i_region_defs * sizeof(dvbsub_region_t) );
688     for( i = 0; i < p_sys->p_page->i_region_defs; i++ )
689     {
690         p_sys->p_page->p_region_defs[i].i_id = bs_read( s, 8 );
691         bs_skip( s, 8 ); /* Reserved */
692         p_sys->p_page->p_region_defs[i].i_x = bs_read( s, 16 );
693         p_sys->p_page->p_region_defs[i].i_y = bs_read( s, 16 );
694
695 #ifdef DEBUG_DVBSUB
696         msg_Dbg( p_dec, "page_composition, region %i (%i,%i)",
697                  i, p_sys->p_page->p_region_defs[i].i_x,
698                  p_sys->p_page->p_region_defs[i].i_y );
699 #endif
700     }
701 }
702
703 static void decode_region_composition( decoder_t *p_dec, bs_t *s )
704 {
705     decoder_sys_t *p_sys = p_dec->p_sys;
706     dvbsub_region_t *p_region, **pp_region = &p_sys->p_regions;
707     int i_segment_length;
708     int i_processed_length;
709     int i_region_id;
710     int i_version_number;
711
712     i_segment_length = bs_read( s, 16 );
713     i_region_id = bs_read( s, 8 );
714     i_version_number = bs_read( s, 4 );
715
716     /* Check if we already have this region */
717     for( p_region = p_sys->p_regions; p_region != NULL;
718          p_region = p_region->p_next )
719     {
720         pp_region = &p_region->p_next;
721         if( p_region->i_id == i_region_id ) break;
722     }
723
724     /* Check version number */
725     if( p_region &&
726         p_region->i_version_number == i_version_number )
727     {
728         bs_skip( s,  8 * (i_segment_length - 1) - 4 );
729         return;
730     }
731     else if( p_region )
732     {
733         if( p_region->i_object_defs )
734             free( p_region->p_object_defs );
735     }
736
737     if( !p_region )
738     {
739 #ifdef DEBUG_DVBSUB
740         msg_Dbg( p_dec, "new region: %i", i_region_id );
741 #endif
742         p_region = *pp_region = malloc( sizeof(dvbsub_region_t) );
743         p_region->p_next = NULL;
744     }
745
746     /* Region attributes */
747     p_region->i_id = i_region_id;
748     p_region->i_version_number = i_version_number;
749     p_region->b_fill           = bs_read( s, 1 );
750     bs_skip( s, 3 ); /* Reserved */
751     p_region->i_width          = bs_read( s, 16 );
752     p_region->i_height         = bs_read( s, 16 );
753     p_region->i_level_comp     = bs_read( s, 3 );
754     p_region->i_depth          = bs_read( s, 3 );
755     bs_skip( s, 2 ); /* Reserved */
756     p_region->i_clut           = bs_read( s, 8 );
757     p_region->i_8bp_code       = bs_read( s, 8 );
758     p_region->i_4bp_code       = bs_read( s, 4 );
759     p_region->i_2bp_code       = bs_read( s, 2 );
760     bs_skip( s, 2 ); /* Reserved */
761     p_region->p_object_defs    = NULL;
762     p_region->i_object_defs    = 0;
763
764     /* List of objects in the region */
765     i_processed_length = 10;
766     while( i_processed_length < i_segment_length )
767     {
768         dvbsub_objectdef_t *p_obj;
769
770         /* We create a new object */
771         p_region->i_object_defs++;
772         p_region->p_object_defs =
773             realloc( p_region->p_object_defs,
774                      sizeof(dvbsub_objectdef_t) * p_region->i_object_defs );
775
776         /* We parse object properties */
777         p_obj = &p_region->p_object_defs[p_region->i_object_defs - 1];
778         p_obj->i_id         = bs_read( s, 16 );
779         p_obj->i_type       = bs_read( s, 2 );
780         p_obj->i_provider   = bs_read( s, 2 );
781         p_obj->i_x          = bs_read( s, 12 );
782         bs_skip( s, 4 ); /* Reserved */
783         p_obj->i_y          = bs_read( s, 12 );
784
785         i_processed_length += 6;
786
787         if( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ||
788             p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING )
789         {
790             p_obj->i_fg_pc =  bs_read( s, 8 );
791             p_obj->i_bg_pc =  bs_read( s, 8 );
792             i_processed_length += 2;
793         }
794     }
795 }
796
797 static dvbsub_image_t *dvbsub_parse_pdata( decoder_t *, bs_t *, uint16_t );
798 static uint16_t dvbsub_pdata2bpp( bs_t *, uint16_t *, dvbsub_image_t *, int );
799 static uint16_t dvbsub_pdata4bpp( bs_t *, uint16_t *, dvbsub_image_t *, int );
800 static uint16_t dvbsub_pdata8bpp( bs_t *, uint16_t *, dvbsub_image_t *, int );
801
802 static void decode_object( decoder_t *p_dec, bs_t *s )
803 {
804     decoder_sys_t   *p_sys = p_dec->p_sys;
805     dvbsub_object_t *p_obj, **pp_obj = &p_sys->p_objects;
806     int i_segment_length;
807     int i_version_number;
808     int i_coding_method;
809     int i_obj_id;
810
811     i_segment_length   = bs_read( s, 16 );
812     i_obj_id           = bs_read( s, 16 );
813     i_version_number   = bs_read( s, 4 );
814     i_coding_method    = bs_read( s, 2 );
815
816     if( i_coding_method )
817     {
818         /* TODO: DVB subtitling as characters */
819         msg_Dbg( p_dec, "DVB subtitling as characters is not handled!" );
820         bs_skip( s,  8 * (i_segment_length - 2) - 6 );
821         return;
822     }
823
824     /* Check if we already have this region */
825     for( p_obj = p_sys->p_objects; p_obj != NULL; p_obj = p_obj->p_next )
826     {
827         pp_obj = &p_obj->p_next;
828         if( p_obj->i_id == i_obj_id ) break;
829     }
830
831     /* Check version number */
832     if( p_obj && p_obj->i_version_number == i_version_number )
833     {
834         bs_skip( s,  8 * (i_segment_length - 2) - 6 );
835         return;
836     }
837     else if( p_obj )
838     {
839         /* Clean structure */
840     }
841
842     if( !p_obj )
843     {
844 #ifdef DEBUG_DVBSUB
845         msg_Dbg( p_dec, "new object: %i", i_obj_id );
846 #endif
847         p_obj = *pp_obj = malloc( sizeof(dvbsub_object_t) );
848         p_obj->p_next = NULL;
849     }
850
851     p_obj->i_id               = i_obj_id;
852     p_obj->i_version_number   = i_version_number;
853     p_obj->i_coding_method    = i_coding_method;
854     p_obj->b_non_modify_color = bs_read( s, 1 );
855     bs_skip( s, 1 ); /* Reserved */
856
857     if( p_obj->i_coding_method == 0x00 )
858     {
859         uint16_t i_topfield_length;
860         uint16_t i_bottomfield_length;
861
862         i_topfield_length    = bs_read( s, 16 );
863         i_bottomfield_length = bs_read( s, 16 );
864
865         p_obj->topfield =
866             dvbsub_parse_pdata( p_dec, s, i_topfield_length );
867         p_obj->bottomfield =
868             dvbsub_parse_pdata( p_dec, s, i_bottomfield_length );
869
870         /* Check word-alignement */
871         bs_align( s );
872         if( bs_pos( s ) % 16 ) bs_skip( s, 8 );
873     }
874     else
875     {
876         /* TODO: DVB subtitling as characters */
877     }
878
879 #ifdef DEBUG_DVBSUB
880         msg_Dbg( p_dec, "end object: %i", i_obj_id );
881 #endif
882 }
883
884 static dvbsub_image_t* dvbsub_parse_pdata( decoder_t *p_dec, bs_t *s,
885                                            uint16_t length )
886 {
887     dvbsub_image_t* p_image;
888     uint16_t i_processed_length = 0;
889     uint16_t i_lines = 0;
890     uint16_t i_cols_last = 0;
891
892     p_image = malloc( sizeof(dvbsub_image_t) );
893     p_image->p_last = p_image->p_codes = NULL;
894
895     memset( p_image->i_cols, 0, 576 * sizeof(uint16_t) );
896
897     /* Let's parse it a first time to determine the size of the buffer */
898     while( i_processed_length < length )
899     {
900         i_processed_length++;
901
902         switch( bs_read( s, 8 ) )
903         {
904             case 0x10:
905                 i_processed_length +=
906                     dvbsub_pdata2bpp( s, &p_image->i_cols[i_lines],
907                                       p_image, length - i_processed_length );
908                 break;
909             case 0x11:
910                 i_processed_length +=
911                     dvbsub_pdata4bpp( s, &p_image->i_cols[i_lines],
912                                       p_image, length - i_processed_length );
913                 break;
914             case 0x12:
915                 i_processed_length +=
916                     dvbsub_pdata8bpp( s, &p_image->i_cols[i_lines],
917                                       p_image, length - i_processed_length );
918                 break;
919             case 0x20:
920             case 0x21:
921             case 0x22:
922                 /* We don't use map tables */
923                 break;
924             case 0xf0:
925                 i_lines++; /* End of line code */
926                 break;
927         }
928     }
929
930     p_image->i_rows = i_lines;
931     p_image->i_cols[i_lines] = i_cols_last;
932
933     return p_image;
934 }
935
936 static void add_rle_code( dvbsub_image_t *p, uint16_t num, uint8_t color,
937                           int i_bpp )
938 {
939     if( p->p_last != NULL )
940     {
941         p->p_last->p_next = malloc( sizeof(dvbsub_rle_t) );
942         p->p_last = p->p_last->p_next;
943     }
944     else
945     {
946         p->p_codes = malloc( sizeof(dvbsub_rle_t) );
947         p->p_last = p->p_codes;
948     }
949     p->p_last->i_num = num;
950
951     p->p_last->i_color_code = color;
952     p->p_last->i_bpp = i_bpp;
953     p->p_last->p_next = NULL;
954 }
955
956 static uint16_t dvbsub_pdata2bpp( bs_t *s, uint16_t* p,
957                                   dvbsub_image_t* p_image, int i_length )
958 {
959     uint16_t i_processed = 0;
960     vlc_bool_t b_stop = 0;
961     uint16_t i_count = 0;
962     uint8_t i_color = 0;
963
964     while( !b_stop && i_processed/8 < i_length )
965     {
966         i_processed += 2;
967         if( (i_color = bs_read( s, 2 )) != 0x00 )
968         {
969             (*p)++;
970
971             /* Add 1 pixel */
972             add_rle_code( p_image, 1, i_color, 2 );
973         }
974         else
975         {
976             i_processed++;
977             if( bs_read( s, 1 ) == 0x01 )         // Switch1
978             {
979                 i_count = 3 + bs_read( s, 3 );
980                 (*p) += i_count ;
981                 i_color = bs_read( s, 2 );
982                 add_rle_code( p_image, i_count, i_color, 2 );
983                 i_processed += 5;
984             }
985             else
986             {
987                 i_processed++;
988                 if( bs_read( s, 1 ) == 0x00 )     //Switch2
989                 {
990                     i_processed += 2;
991                     switch( bs_read( s, 2 ) )     //Switch3
992                     {
993                     case 0x00:
994                         b_stop=1;
995                         break;
996                     case 0x01:
997                         (*p) += 2 ;
998                         add_rle_code( p_image, 2, 0, 2 );
999                         break;
1000                     case 0x02:
1001                         i_count =  12 + bs_read( s, 4 );
1002                         i_color = bs_read( s, 2 );
1003                         (*p) += i_count;
1004                         i_processed += 6;
1005                         add_rle_code( p_image, i_count, i_color, 2 );
1006                         break;
1007                     case 0x03:
1008                         i_count =  29 + bs_read( s, 8 );
1009                         i_color = bs_read( s, 2 );
1010                         (*p) += i_count;
1011                         i_processed += 10;
1012                         add_rle_code( p_image, i_count, i_color, 2 );
1013                         break;
1014                     default:
1015                         break;
1016                     }
1017                 }
1018                 else
1019                 {
1020                     (*p)++;
1021                     add_rle_code( p_image, 1, 0, 2 ); /* 1 pixel color 0 */
1022                 }
1023             }
1024         }
1025     }
1026
1027     bs_align( s );
1028
1029     return ( i_processed + 7 ) / 8;
1030 }
1031
1032 static uint16_t dvbsub_pdata4bpp( bs_t *s, uint16_t* p,
1033                                   dvbsub_image_t* p_image, int i_length )
1034 {
1035     uint16_t i_processed = 0;
1036     vlc_bool_t b_stop = 0;
1037     uint16_t i_count = 0;
1038     uint8_t i_color = 0;
1039
1040     while( !b_stop && i_processed/8 < i_length )
1041     {
1042         if( (i_color = bs_read( s, 4 )) != 0x00 )
1043         {
1044             (*p)++;
1045             i_processed+=4;
1046
1047             /* Add 1 pixel */
1048             add_rle_code( p_image, 1, i_color, 4 );
1049         }
1050         else
1051         {
1052             if( bs_read( s, 1 ) == 0x00 )           // Switch1
1053             {
1054                 if( bs_show( s, 3 ) != 0x00 )
1055                 {
1056                     i_count = 2 + bs_read( s, 3 );
1057                     (*p) += i_count ;
1058                     add_rle_code( p_image, i_count, 0x00, 4 );
1059                 }
1060                 else
1061                 {
1062                     bs_skip( s, 3 );
1063                     b_stop=1;
1064                 }
1065                 i_processed += 8;
1066             }
1067             else
1068             {
1069                 if( bs_read( s, 1 ) == 0x00)        //Switch2
1070                 {
1071                     i_count =  4 + bs_read( s, 2 );
1072                     i_color = bs_read( s, 4 );
1073                     (*p) += i_count;
1074                     i_processed += 12;
1075                     add_rle_code( p_image, i_count, i_color, 4 );
1076                 }
1077                 else
1078                 {
1079                     switch ( bs_read( s, 2 ) )     //Switch3
1080                     {
1081                         case 0x0:
1082                             (*p)++;
1083                             i_processed += 8;
1084                             add_rle_code( p_image, 1, 0x00, 4 );
1085                             break;
1086                         case 0x1:
1087                             (*p)+=2;
1088                             i_processed += 8;
1089                             add_rle_code( p_image, 2, 0x00, 4 );
1090                             break;
1091                         case 0x2:
1092                              i_count = 9 + bs_read( s, 4 );
1093                              i_color = bs_read( s, 4 );
1094                              (*p)+= i_count;
1095                              i_processed += 16;
1096                              add_rle_code( p_image, i_count, i_color, 4 );
1097                              break;
1098                         case 0x3:
1099                              i_count= 25 + bs_read( s, 8 );
1100                              i_color = bs_read( s, 4 );
1101                              (*p)+= i_count;
1102                              i_processed += 20;
1103                              add_rle_code( p_image, i_count, i_color, 4 );
1104                              break;
1105                     }
1106                 }
1107             }
1108         }
1109     }
1110
1111     bs_align( s );
1112
1113     return ( i_processed + 7 ) / 8 ;
1114 }
1115
1116 static uint16_t dvbsub_pdata8bpp( bs_t *s, uint16_t* p,
1117                                   dvbsub_image_t* p_image, int i_length )
1118 {
1119     uint16_t i_processed = 0;
1120     vlc_bool_t b_stop = 0;
1121     uint16_t i_count = 0;
1122     uint8_t i_color = 0;
1123
1124     while( !b_stop && i_processed/8 < i_length )
1125     {
1126         i_processed += 8;
1127         if( (i_color = bs_read( s, 8 )) != 0x00 )
1128         {
1129             (*p)++;
1130
1131             /* Add 1 pixel */
1132             add_rle_code( p_image, 1, i_color, 8 );
1133         }
1134         else
1135         {
1136             i_processed++;
1137             if( bs_read( s, 1 ) == 0x00 )           // Switch1
1138             {
1139                 if( bs_show( s, 7 ) != 0x00 )
1140                 {
1141                     i_count = bs_read( s, 7 );
1142                     (*p) += i_count ;
1143                     add_rle_code( p_image, i_count, 0x00, 8 );
1144                 }
1145                 else
1146                 {
1147                     bs_skip( s, 7 );
1148                     b_stop = 1;
1149                 }
1150                 i_processed += 7;
1151             }
1152             else
1153             {
1154                 i_count = bs_read( s, 7 );
1155                 (*p) += i_count ;
1156                 i_color = bs_read( s, 8 );
1157                 add_rle_code( p_image, i_count, i_color, 8 );
1158                 i_processed += 15;
1159             }
1160         }
1161     }
1162
1163     bs_align( s );
1164
1165     return ( i_processed + 7 ) / 8 ;
1166 }
1167
1168 static void free_image( dvbsub_image_t *p_i )
1169 {
1170     dvbsub_rle_t *p1;
1171     dvbsub_rle_t *p2 = NULL;
1172
1173     for( p1 = p_i->p_codes; p1 != NULL; p1 = p2 )
1174     {
1175         p2 = p1->p_next;
1176         free( p1 );
1177         p1 = NULL;
1178     }
1179
1180     free( p_i );
1181 }
1182
1183 static void free_objects( decoder_t *p_dec )
1184 {
1185     decoder_sys_t *p_sys = p_dec->p_sys;
1186     dvbsub_object_t *p_obj, *p_obj_next;
1187
1188     for( p_obj = p_sys->p_objects; p_obj != NULL; p_obj = p_obj_next )
1189     {
1190         p_obj_next = p_obj->p_next;
1191         free_image( p_obj->topfield );
1192         free_image( p_obj->bottomfield );
1193         free( p_obj );
1194     }
1195     p_sys->p_objects = NULL;
1196 }
1197
1198 static void free_all( decoder_t *p_dec )
1199 {
1200     decoder_sys_t *p_sys = p_dec->p_sys;
1201     dvbsub_region_t *p_reg, *p_reg_next;
1202     int i;
1203
1204     for( i = 0; i < 256; i++ )
1205     {
1206         if( p_sys->p_clut[i] ) free( p_sys->p_clut[i] );
1207         p_sys->p_clut[i] = NULL;
1208     }
1209
1210     if( p_sys->p_page )
1211     {
1212         if( p_sys->p_page->i_region_defs )
1213             free( p_sys->p_page->p_region_defs );
1214         free( p_sys->p_page );
1215         p_sys->p_page = NULL;
1216     }
1217
1218     for( p_reg = p_sys->p_regions; p_reg != NULL; p_reg = p_reg_next )
1219     {
1220         p_reg_next = p_reg->p_next;
1221         if( p_reg->i_object_defs ) free( p_reg->p_object_defs );
1222         free( p_reg );
1223     }
1224     p_sys->p_regions = NULL;
1225
1226     free_objects( p_dec );
1227 }
1228
1229 static subpicture_t *render( decoder_t *p_dec )
1230 {
1231     decoder_sys_t   *p_sys = p_dec->p_sys;
1232     dvbsub_clut_t   *p_clut;
1233     dvbsub_rle_t    *p_c;
1234     subpicture_t    *p_spu;
1235     subpicture_region_t **pp_spu_region;
1236     int i, j = 0, i_timeout = 0;
1237
1238     /* Allocate the subpicture internal data. */
1239     p_spu = p_dec->pf_spu_buffer_new( p_dec );
1240     if( !p_spu ) return NULL;
1241
1242     pp_spu_region = &p_spu->p_region;
1243
1244     /* Loop on region definitions */
1245 #ifdef DEBUG_DVBSUB
1246     if( p_sys->p_page )
1247         msg_Dbg( p_dec, "rendering %i regions", p_sys->p_page->i_region_defs );
1248 #endif
1249
1250     for( i = 0; p_sys->p_page && i < p_sys->p_page->i_region_defs; i++ )
1251     {
1252         dvbsub_region_t     *p_region;
1253         dvbsub_regiondef_t  *p_regiondef;
1254         subpicture_region_t *p_spu_region;
1255         dvbsub_color_t      *p_color;
1256         video_format_t fmt;
1257         uint8_t *p_y;
1258         int i_pitch, i_background;
1259
1260         i_timeout = p_sys->p_page->i_timeout;
1261
1262         p_regiondef = &p_sys->p_page->p_region_defs[i];
1263
1264 #ifdef DEBUG_DVBSUB
1265         msg_Dbg( p_dec, "rendering region %i (%i,%i)", i,
1266                  p_regiondef->i_x, p_regiondef->i_y );
1267 #endif
1268
1269         /* Find associated region */
1270         for( p_region = p_sys->p_regions; p_region != NULL;
1271              p_region = p_region->p_next )
1272         {
1273             if( p_regiondef->i_id == p_region->i_id ) break;
1274         }
1275
1276         if( !p_region )
1277         {
1278             msg_Err( p_dec, "no region founddddd!!!" );
1279             continue;
1280         }
1281
1282         /* Create new SPU region */
1283         memset( &fmt, 0, sizeof(video_format_t) );
1284         fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
1285         fmt.i_aspect = VOUT_ASPECT_FACTOR;
1286         fmt.i_width = fmt.i_visible_width = p_region->i_width;
1287         fmt.i_height = fmt.i_visible_height = p_region->i_height;
1288         fmt.i_x_offset = fmt.i_y_offset = 0;
1289         p_spu_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
1290         if( !p_region )
1291         {
1292             msg_Err( p_dec, "cannot allocate SPU region" );
1293             continue;
1294         }
1295         p_spu_region->i_x = p_regiondef->i_x;
1296         p_spu_region->i_y = p_regiondef->i_y;
1297         *pp_spu_region = p_spu_region;
1298         pp_spu_region = &p_spu_region->p_next;
1299
1300         /* Build palette */
1301         p_clut = p_sys->p_clut[p_region->i_clut];
1302         if( !p_clut ) p_clut = &p_sys->default_clut;
1303         fmt.p_palette->i_entries = p_region->i_depth == 1 ? 4 :
1304             p_region->i_depth == 2 ? 16 : 256;
1305         p_color = (p_region->i_depth == 1) ? p_clut->c_2b :
1306             (p_region->i_depth == 2) ? p_clut->c_4b : p_clut->c_8b;
1307         for( j = 0; j < fmt.p_palette->i_entries; j++ )
1308         {
1309             fmt.p_palette->palette[j][0] = p_color[j].Y;
1310             fmt.p_palette->palette[j][1] = p_color[j].Cr;
1311             fmt.p_palette->palette[j][2] = p_color[j].Cb;
1312             fmt.p_palette->palette[j][3] = 0xff - p_color[j].T;
1313         }
1314
1315         p_y = p_spu_region->picture.Y_PIXELS;
1316         i_pitch = p_spu_region->picture.Y_PITCH;
1317
1318         /* Erase region */
1319         i_background = (p_region->i_depth == 1) ? p_region->i_2bp_code :
1320             (p_region->i_depth == 2) ? p_region->i_4bp_code :
1321             p_region->i_8bp_code;
1322         memset( p_y, i_background, i_pitch * p_region->i_height );
1323
1324         /* Loop on object definitions */
1325         for( j = 0; j < p_region->i_object_defs; j++ )
1326         {
1327             dvbsub_object_t    *p_object;
1328             dvbsub_objectdef_t *p_objectdef;
1329             uint16_t k, l, x, y;
1330
1331             p_objectdef = &p_region->p_object_defs[j];
1332
1333 #ifdef DEBUG_DVBSUB
1334             msg_Dbg( p_dec, "rendering object %i (%i,%i)", p_objectdef->i_id,
1335                      p_objectdef->i_x, p_objectdef->i_y );
1336 #endif
1337
1338             /* Look for the right object */
1339             for( p_object = p_sys->p_objects; p_object != NULL;
1340                  p_object = p_object->p_next )
1341             {
1342                 if( p_objectdef->i_id == p_object->i_id ) break;
1343             }
1344
1345             if( !p_object )
1346             {
1347                 msg_Err( p_dec, "no object founddddd!!!" );
1348                 continue;
1349             }
1350
1351             /* Draw SPU region */
1352             for( k = 0, l = 0, p_c = p_object->topfield->p_codes;
1353                  p_c; p_c = p_c->p_next )
1354             {
1355                 x = l + p_objectdef->i_x;
1356                 y = 2 * k + p_objectdef->i_y;
1357                 memset( p_y + y * i_pitch + x, p_c->i_color_code, p_c->i_num );
1358
1359                 l += p_c->i_num;
1360                 if( l >= p_object->topfield->i_cols[k] ) { k++; l = 0; }
1361                 if( k >= p_object->topfield->i_rows) break;
1362             }
1363
1364             for( k = 0, l = 0, p_c = p_object->bottomfield->p_codes;
1365                  p_c; p_c = p_c->p_next )
1366             {
1367                 x = l + p_objectdef->i_x;
1368                 y = 2 * k + 1 + p_objectdef->i_y;
1369                 memset( p_y + y * i_pitch + x, p_c->i_color_code, p_c->i_num );
1370
1371                 l += p_c->i_num;
1372                 if( l >= p_object->bottomfield->i_cols[k] ) { k++; l = 0; }
1373                 if( k >= p_object->bottomfield->i_rows) break;
1374
1375             }
1376         }
1377     }
1378
1379     /* Set the pf_render callback */
1380     p_spu->i_start = p_sys->i_pts;
1381     p_spu->i_stop = p_spu->i_start + i_timeout * 1000000;
1382     p_spu->b_ephemer = VLC_TRUE;
1383
1384     return p_spu;
1385 }
1386
1387 /*****************************************************************************
1388  * encoder_sys_t : encoder descriptor
1389  *****************************************************************************/
1390 struct encoder_sys_t
1391 {
1392     unsigned int i_page_ver;
1393     unsigned int i_region_ver;
1394     unsigned int i_clut_ver;
1395
1396     /*
1397      * Input properties
1398      */
1399     /*
1400      * Common properties
1401      */
1402     mtime_t i_pts;
1403 };
1404
1405 static void encode_page_composition( encoder_t *, bs_t *, subpicture_t * );
1406 static void encode_clut( encoder_t *, bs_t *, subpicture_t * );
1407 static void encode_region_composition( encoder_t *, bs_t *, subpicture_t * );
1408 static void encode_object( encoder_t *, bs_t *, subpicture_t * );
1409
1410 /*****************************************************************************
1411  * OpenEncoder: probe the encoder and return score
1412  *****************************************************************************/
1413 static int OpenEncoder( vlc_object_t *p_this )
1414 {
1415     encoder_t *p_enc = (encoder_t *)p_this;
1416     encoder_sys_t *p_sys;
1417
1418     if( p_enc->fmt_out.i_codec != VLC_FOURCC('d','v','b','s') &&
1419         !p_enc->b_force )
1420     {
1421         return VLC_EGENERIC;
1422     }
1423
1424     /* Allocate the memory needed to store the decoder's structure */
1425     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1426     {
1427         msg_Err( p_enc, "out of memory" );
1428         return VLC_EGENERIC;
1429     }
1430     p_enc->p_sys = p_sys;
1431
1432     p_enc->pf_encode_sub = Encode;
1433     p_enc->fmt_out.i_codec = VLC_FOURCC('d','v','b','s');
1434     p_enc->fmt_out.subs.dvb.i_id  = 1 << 16 | 1;
1435
1436     sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
1437
1438     p_sys->i_page_ver = 0;
1439     p_sys->i_region_ver = 0;
1440     p_sys->i_clut_ver = 0;
1441
1442     return VLC_SUCCESS;
1443 }
1444
1445 /****************************************************************************
1446  * Encode: the whole thing
1447  ****************************************************************************/
1448 static block_t *Encode( encoder_t *p_enc, subpicture_t *p_subpic )
1449 {
1450     bs_t bits, *s = &bits;
1451     block_t *p_block;
1452
1453     if( !p_subpic || !p_subpic->p_region ) return 0;
1454
1455     msg_Dbg( p_enc, "encoding subpicture" );
1456
1457     p_block = block_New( p_enc, 64000 );
1458     bs_init( s, p_block->p_buffer, p_block->i_buffer );
1459
1460     bs_write( s, 8, 0x20 ); /* Data identifier */
1461     bs_write( s, 8, 0x0 ); /* Subtitle stream id */
1462
1463     encode_page_composition( p_enc, s, p_subpic );
1464     encode_region_composition( p_enc, s, p_subpic );
1465     encode_clut( p_enc, s, p_subpic );
1466     encode_object( p_enc, s, p_subpic );
1467
1468     /* End of display */
1469     bs_write( s, 8, 0x0f ); /* Sync byte */
1470     bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
1471     bs_write( s, 16, 1 ); /* Page id */
1472     bs_write( s, 16, 0 ); /* Segment length */
1473
1474     bs_write( s, 8, 0xff ); /* End marker */
1475     p_block->i_buffer = bs_pos( s ) / 8;
1476     p_block->i_pts = p_block->i_dts = p_subpic->i_start;
1477     if( !p_subpic->b_ephemer && p_subpic->i_stop )
1478         p_block->i_length = p_subpic->i_stop - p_subpic->i_start;
1479
1480     msg_Dbg( p_enc, "subpicture encoded properly" );
1481
1482     return p_block;
1483 }
1484
1485 /*****************************************************************************
1486  * CloseEncoder: encoder destruction
1487  *****************************************************************************/
1488 static void CloseEncoder( vlc_object_t *p_this )
1489 {
1490     encoder_t *p_enc = (encoder_t *)p_this;
1491     encoder_sys_t *p_sys = p_enc->p_sys;
1492
1493     free( p_sys );
1494 }
1495
1496 static void encode_page_composition( encoder_t *p_enc, bs_t *s,
1497                                      subpicture_t *p_subpic )
1498 {
1499     encoder_sys_t *p_sys = p_enc->p_sys;
1500     subpicture_region_t *p_region;
1501     int i_regions;
1502
1503     bs_write( s, 8, 0x0f ); /* Sync byte */
1504     bs_write( s, 8, DVBSUB_ST_PAGE_COMPOSITION ); /* Segment type */
1505     bs_write( s, 16, 1 ); /* Page id */
1506
1507     for( i_regions = 0, p_region = p_subpic->p_region; p_region;
1508          p_region = p_region->p_next, i_regions++ );
1509
1510     bs_write( s, 16, i_regions * 6 + 2 ); /* Segment length */
1511
1512     bs_write( s, 8, 5 ); /* Timeout */
1513     bs_write( s, 4, p_sys->i_page_ver++ );
1514     bs_write( s, 2, DVBSUB_PCS_STATE_ACQUISITION );
1515     bs_write( s, 2, 0 ); /* Reserved */
1516
1517     for( i_regions = 0, p_region = p_subpic->p_region; p_region;
1518          p_region = p_region->p_next, i_regions++ )
1519     {
1520         bs_write( s, 8, i_regions );
1521         bs_write( s, 8, 0 ); /* Reserved */
1522         bs_write( s, 16, p_region->i_x );
1523         bs_write( s, 16, p_region->i_y );
1524     }
1525 }
1526
1527 static void encode_clut( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
1528 {
1529     encoder_sys_t *p_sys = p_enc->p_sys;
1530     subpicture_region_t *p_region = p_subpic->p_region;
1531     video_palette_t *p_pal;
1532     int i;
1533
1534     /* Sanity check */
1535     if( !p_region || !p_region->fmt.p_palette ||
1536         p_region->fmt.i_chroma != VLC_FOURCC('Y','U','V','P') ) return;
1537
1538     bs_write( s, 8, 0x0f ); /* Sync byte */
1539     bs_write( s, 8, DVBSUB_ST_CLUT_DEFINITION ); /* Segment type */
1540     bs_write( s, 16, 1 ); /* Page id */
1541
1542     p_pal = p_region->fmt.p_palette;
1543
1544     bs_write( s, 16, p_pal->i_entries * 6 + 2 ); /* Segment length */
1545     bs_write( s, 8, 1 ); /* Clut id */
1546     bs_write( s, 4, p_sys->i_clut_ver++ );
1547     bs_write( s, 4, 0 ); /* Reserved */
1548
1549     for( i = 0; i < p_pal->i_entries; i++ )
1550     {
1551         bs_write( s, 8, i ); /* Clut entry id */
1552         bs_write( s, 1, p_pal->i_entries == 4 );   /* 2bit/entry flag */
1553         bs_write( s, 1, p_pal->i_entries == 16 );  /* 4bit/entry flag */
1554         bs_write( s, 1, p_pal->i_entries == 256 ); /* 8bit/entry flag */
1555         bs_write( s, 4, 0 ); /* Reserved */
1556         bs_write( s, 1, 1 ); /* Full range flag */
1557         bs_write( s, 8, p_pal->palette[i][0] ); /* Y value */
1558         bs_write( s, 8, p_pal->palette[i][1] ); /* Cr value */
1559         bs_write( s, 8, p_pal->palette[i][2] ); /* Cb value */
1560         bs_write( s, 8, 0xff - p_pal->palette[i][3] ); /* T value */
1561     }
1562 }
1563
1564 static void encode_region_composition( encoder_t *p_enc, bs_t *s,
1565                                        subpicture_t *p_subpic )
1566 {
1567     encoder_sys_t *p_sys = p_enc->p_sys;
1568     subpicture_region_t *p_region;
1569     int i_regions;
1570
1571     for( i_regions = 0, p_region = p_subpic->p_region; p_region;
1572          p_region = p_region->p_next, i_regions++ )
1573     {
1574         video_palette_t *p_pal = p_region->fmt.p_palette;
1575         int i_depth = p_pal->i_entries == 4 ? 0x1 :
1576             p_pal->i_entries == 16 ? 0x2 : 0x3;
1577
1578         bs_write( s, 8, 0x0f ); /* Sync byte */
1579         bs_write( s, 8, DVBSUB_ST_REGION_COMPOSITION ); /* Segment type */
1580         bs_write( s, 16, 1 ); /* Page id */
1581
1582         bs_write( s, 16, 10 + 6 ); /* Segment length */
1583         bs_write( s, 8, i_regions );
1584         bs_write( s, 4, p_sys->i_region_ver++ );
1585
1586         /* Region attributes */
1587         bs_write( s, 1, 0 ); /* Fill */
1588         bs_write( s, 3, 0 ); /* Reserved */
1589         bs_write( s, 16, p_region->fmt.i_visible_width );
1590         bs_write( s, 16, p_region->fmt.i_visible_height );
1591         bs_write( s, 3, i_depth );  /* Region level of compatibility */
1592         bs_write( s, 3, i_depth  ); /* Region depth */
1593         bs_write( s, 2, 0 ); /* Reserved */
1594         bs_write( s, 8, 1 ); /* Clut id */
1595         bs_write( s, 8, 0 ); /* region 8bit pixel code */
1596         bs_write( s, 4, 0 ); /* region 4bit pixel code */
1597         bs_write( s, 2, 0 ); /* region 2bit pixel code */
1598         bs_write( s, 2, 0 ); /* Reserved */
1599
1600         /* In our implementation we only have 1 object per region */
1601         bs_write( s, 16, i_regions );
1602         bs_write( s, 2, DVBSUB_OT_BASIC_BITMAP );
1603         bs_write( s, 2, 0 ); /* object provider flag */
1604         bs_write( s, 12, 0 ); /* object horizontal position */
1605         bs_write( s, 4, 0 ); /* Reserved */
1606         bs_write( s, 12, 0 ); /* object vertical position */
1607     }
1608 }
1609
1610 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
1611                                subpicture_region_t *p_region,
1612                                vlc_bool_t b_top );
1613
1614 static void encode_object( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
1615 {
1616     encoder_sys_t   *p_sys = p_enc->p_sys;
1617     subpicture_region_t *p_region;
1618     int i_regions;
1619
1620     int i_length_pos, i_update_pos, i_pixel_data_pos;
1621
1622     for( i_regions = 0, p_region = p_subpic->p_region; p_region;
1623          p_region = p_region->p_next, i_regions++ )
1624     {
1625         bs_write( s, 8, 0x0f ); /* Sync byte */
1626         bs_write( s, 8, DVBSUB_ST_OBJECT_DATA ); /* Segment type */
1627         bs_write( s, 16, 1 ); /* Page id */
1628
1629         i_length_pos = bs_pos( s );
1630         bs_write( s, 16, 0 ); /* Segment length */
1631         bs_write( s, 16, i_regions ); /* Object id */
1632         bs_write( s, 4, p_sys->i_region_ver++ );
1633         bs_write( s, 2, 0 ); /* object coding method */
1634
1635         bs_write( s, 1, 0 ); /* non modifying color flag */
1636         bs_write( s, 1, 0 ); /* Reserved */
1637
1638         i_update_pos = bs_pos( s );
1639         bs_write( s, 16, 0 ); /* topfield data block length */
1640         bs_write( s, 16, 0 ); /* bottomfield data block length */
1641
1642         /* Top field */
1643         i_pixel_data_pos = bs_pos( s );
1644         encode_pixel_data( p_enc, s, p_region, VLC_TRUE );
1645         i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
1646         SetWBE( &s->p_start[i_update_pos/8], i_pixel_data_pos );
1647
1648         /* Bottom field */
1649         i_pixel_data_pos = bs_pos( s );
1650         encode_pixel_data( p_enc, s, p_region, VLC_FALSE );
1651         i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
1652         SetWBE( &s->p_start[i_update_pos/8+2], i_pixel_data_pos );
1653
1654         /* Stuffing for word alignment */
1655         bs_align_0( s );
1656         if( bs_pos( s ) % 16 ) bs_write( s, 8, 0 );
1657
1658         /* Update segment length */
1659         SetWBE( &s->p_start[i_length_pos/8], (bs_pos(s) - i_length_pos -2)/8 );
1660     }
1661 }
1662
1663 static void encode_pixel_line_2bp( encoder_t *p_enc, bs_t *s,
1664                                    subpicture_region_t *p_region,
1665                                    int i_line );
1666 static void encode_pixel_line_4bp( encoder_t *p_enc, bs_t *s,
1667                                    subpicture_region_t *p_region,
1668                                    int i_line );
1669 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
1670                                subpicture_region_t *p_region,
1671                                vlc_bool_t b_top )
1672 {
1673     unsigned int i_line;
1674
1675     /* Sanity check */
1676     if( p_region->fmt.i_chroma != VLC_FOURCC('Y','U','V','P') ) return;
1677
1678     /* Encode line by line */
1679     for( i_line = !b_top; i_line < p_region->fmt.i_visible_height;
1680          i_line += 2 )
1681     {
1682         switch( p_region->fmt.p_palette->i_entries )
1683         {
1684         case 4:
1685             bs_write( s, 8, 0x10 ); /* 2 bit/pixel code string */
1686             encode_pixel_line_2bp( p_enc, s, p_region, i_line );
1687             break;
1688
1689         case 16:
1690             bs_write( s, 8, 0x11 ); /* 4 bit/pixel code string */
1691             encode_pixel_line_4bp( p_enc, s, p_region, i_line );
1692             break;
1693
1694         default:
1695             msg_Err( p_enc, "subpicture palette (%i) not handled",
1696                      p_region->fmt.p_palette->i_entries );
1697             break;
1698         }
1699
1700         bs_write( s, 8, 0xf0 ); /* End of object line code */
1701     }
1702 }
1703
1704 static void encode_pixel_line_2bp( encoder_t *p_enc, bs_t *s,
1705                                    subpicture_region_t *p_region,
1706                                    int i_line )
1707 {
1708     unsigned int i, i_length = 0;
1709     int i_pitch = p_region->picture.p->i_pitch;
1710     uint8_t *p_data = &p_region->picture.p->p_pixels[ i_pitch * i_line ];
1711     int i_last_pixel = p_data[0];
1712
1713     for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
1714     {
1715         if( i != p_region->fmt.i_visible_width &&
1716             p_data[i] == i_last_pixel && i_length != 284 )
1717         {
1718             i_length++;
1719             continue;
1720         }
1721
1722         if( i_length == 1 || i_length == 11 || i_length == 28 )
1723         {
1724             /* 2bit/pixel code */
1725             if( i_last_pixel ) bs_write( s, 2, i_last_pixel );
1726             else
1727             {
1728                 bs_write( s, 2, 0 );
1729                 bs_write( s, 1, 0 );
1730                 bs_write( s, 1, 1 ); /* pseudo color 0 */
1731             }
1732             i_length--;
1733         }
1734
1735         if( i_length == 2 )
1736         {
1737             if( i_last_pixel )
1738             {
1739                 bs_write( s, 2, i_last_pixel );
1740                 bs_write( s, 2, i_last_pixel );
1741             }
1742             else
1743             {
1744                 bs_write( s, 2, 0 );
1745                 bs_write( s, 1, 0 );
1746                 bs_write( s, 1, 0 );
1747                 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
1748             }
1749         }
1750         else if( i_length > 2 )
1751         {
1752             bs_write( s, 2, 0 );
1753             if( i_length <= 10 )
1754             {
1755                 bs_write( s, 1, 1 );
1756                 bs_write( s, 3, i_length - 3 );
1757                 bs_write( s, 2, i_last_pixel );
1758             }
1759             else
1760             {
1761                 bs_write( s, 1, 0 );
1762                 bs_write( s, 1, 0 );
1763
1764                 if( i_length <= 27 )
1765                 {
1766                     bs_write( s, 2, 2 );
1767                     bs_write( s, 4, i_length - 12 );
1768                     bs_write( s, 2, i_last_pixel );
1769                 }
1770                 else
1771                 {
1772                     bs_write( s, 2, 3 );
1773                     bs_write( s, 8, i_length - 29 );
1774                     bs_write( s, 2, i_last_pixel );
1775                 }
1776             }
1777         }
1778
1779         if( i == p_region->fmt.i_visible_width ) break;
1780
1781         i_last_pixel = p_data[i];
1782         i_length = 1;
1783     }
1784
1785     /* Stop */
1786     bs_write( s, 2, 0 );
1787     bs_write( s, 1, 0 );
1788     bs_write( s, 1, 0 );
1789     bs_write( s, 2, 0 );
1790
1791     /* Stuffing */
1792     bs_align_0( s );
1793 }
1794
1795 static void encode_pixel_line_4bp( encoder_t *p_enc, bs_t *s,
1796                                    subpicture_region_t *p_region,
1797                                    int i_line )
1798 {
1799     unsigned int i, i_length = 0;
1800     int i_pitch = p_region->picture.p->i_pitch;
1801     uint8_t *p_data = &p_region->picture.p->p_pixels[ i_pitch * i_line ];
1802     int i_last_pixel = p_data[0];
1803
1804     for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
1805     {
1806         if( i != p_region->fmt.i_visible_width &&
1807             p_data[i] == i_last_pixel && i_length != 1 )
1808         {
1809             i_length++;
1810             continue;
1811         }
1812
1813         if( i_length == 1 )
1814         {
1815             /* 4bit/pixel code */
1816             if( i_last_pixel ) bs_write( s, 4, i_last_pixel );
1817             else
1818             {
1819                 bs_write( s, 4, 0 );
1820                 bs_write( s, 1, 1 );
1821                 bs_write( s, 1, 1 );
1822                 bs_write( s, 2, 0 ); /* pseudo color 0 */
1823             }
1824         }
1825
1826         if( i == p_region->fmt.i_visible_width ) break;
1827
1828         i_last_pixel = p_data[i];
1829         i_length = 1;
1830     }
1831
1832     /* Stop */
1833     bs_write( s, 8, 0 );
1834
1835     /* Stuffing */
1836     bs_align_0( s );
1837 }