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