]> git.sesse.net Git - vlc/blob - modules/codec/dvbsub.c
42769f40348d276ecb4585aed39ab596914bc6af
[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, *p_next;
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     /* Initialize to default clut */
507     p_next = p_clut->p_next;
508     *p_clut = p_sys->default_clut;
509     p_clut->p_next = p_next;
510
511     /* We don't have this version of the CLUT: Parse it */
512     p_clut->i_version = i_version;
513     p_clut->i_id = i_id;
514     bs_skip( s, 4 ); /* Reserved bits */
515     i_processed_length = 2;
516     while( i_processed_length < i_segment_length )
517     {
518         uint8_t y, cb, cr, t;
519         uint8_t i_id;
520         uint8_t i_type;
521
522         i_id = bs_read( s, 8 );
523         i_type = bs_read( s, 3 );
524
525         bs_skip( s, 4 );
526
527         if( bs_read( s, 1 ) )
528         {
529             y  = bs_read( s, 8 );
530             cr = bs_read( s, 8 );
531             cb = bs_read( s, 8 );
532             t  = bs_read( s, 8 );
533             i_processed_length += 6;
534         }
535         else
536         {
537             y  = bs_read( s, 6 ) << 2;
538             cr = bs_read( s, 4 ) << 4;
539             cb = bs_read( s, 4 ) << 4;
540             t  = bs_read( s, 2 ) << 6;
541             i_processed_length += 4;
542         }
543
544         /* We are not entirely compliant here as full transparency is indicated
545          * with a luma value of zero, not a transparency value of 0xff
546          * (full transparency would actually be 0xff + 1). */
547
548         if( y == 0 )
549         {
550             cr = cb = 0;
551             t  = 0xff;
552         }
553
554         /* According to EN 300-743 section 7.2.3 note 1, type should
555          * not have more than 1 bit set to one, but some streams don't
556          * respect this note. */
557
558         if( i_type & 0x04 && i_id < 4 )
559         {
560             p_clut->c_2b[i_id].Y = y;
561             p_clut->c_2b[i_id].Cr = cr;
562             p_clut->c_2b[i_id].Cb = cb;
563             p_clut->c_2b[i_id].T = t;
564         }
565         if( i_type & 0x02 && i_id < 16 )
566         {
567             p_clut->c_4b[i_id].Y = y;
568             p_clut->c_4b[i_id].Cr = cr;
569             p_clut->c_4b[i_id].Cb = cb;
570             p_clut->c_4b[i_id].T = t;
571         }
572         if( i_type & 0x01 )
573         {
574             p_clut->c_8b[i_id].Y = y;
575             p_clut->c_8b[i_id].Cr = cr;
576             p_clut->c_8b[i_id].Cb = cb;
577             p_clut->c_8b[i_id].T = t;
578         }
579     }
580 }
581
582 static void decode_page_composition( decoder_t *p_dec, bs_t *s )
583 {
584     decoder_sys_t *p_sys = p_dec->p_sys;
585     int i_version, i_state, i_segment_length, i_timeout, i;
586
587     /* A page is composed by 0 or more region */
588
589     i_segment_length = bs_read( s, 16 );
590     i_timeout = bs_read( s, 8 );
591     i_version = bs_read( s, 4 );
592     i_state = bs_read( s, 2 );
593     bs_skip( s, 2 ); /* Reserved */
594
595     if( i_state == DVBSUB_PCS_STATE_CHANGE )
596     {
597         /* End of an epoch, reset decoder buffer */
598 #ifdef DEBUG_DVBSUB
599         msg_Dbg( p_dec, "page composition mode change" );
600 #endif
601         free_all( p_dec );
602     }
603     else if( !p_sys->p_page && i_state != DVBSUB_PCS_STATE_ACQUISITION &&
604              i_state != DVBSUB_PCS_STATE_CHANGE )
605     {
606         /* Not a full PCS, we need to wait for one */
607         msg_Dbg( p_dec, "didn't receive an acquisition page yet" );
608
609 #if 0 /* Try to start decoding even without an acquisition page */
610         bs_skip( s,  8 * (i_segment_length - 2) );
611         return;
612 #endif
613     }
614
615 #ifdef DEBUG_DVBSUB
616     if( i_state == DVBSUB_PCS_STATE_ACQUISITION )
617         msg_Dbg( p_dec, "acquisition page composition" );
618 #endif
619
620     /* Check version number */
621     if( p_sys->p_page && p_sys->p_page->i_version == i_version )
622     {
623         bs_skip( s,  8 * (i_segment_length - 2) );
624         return;
625     }
626     else if( p_sys->p_page )
627     {
628         if( p_sys->p_page->i_region_defs )
629             free( p_sys->p_page->p_region_defs );
630         p_sys->p_page->i_region_defs = 0;
631     }
632
633     if( !p_sys->p_page )
634     {
635 #ifdef DEBUG_DVBSUB
636         msg_Dbg( p_dec, "new page" );
637 #endif
638         /* Allocate a new page */
639         p_sys->p_page = malloc( sizeof(dvbsub_page_t) );
640     }
641
642     p_sys->p_page->i_version = i_version;
643     p_sys->p_page->i_timeout = i_timeout;
644
645     /* Number of regions */
646     p_sys->p_page->i_region_defs = (i_segment_length - 2) / 6;
647
648     if( p_sys->p_page->i_region_defs == 0 ) return;
649
650     p_sys->p_page->p_region_defs =
651         malloc( p_sys->p_page->i_region_defs * sizeof(dvbsub_region_t) );
652     for( i = 0; i < p_sys->p_page->i_region_defs; i++ )
653     {
654         p_sys->p_page->p_region_defs[i].i_id = bs_read( s, 8 );
655         bs_skip( s, 8 ); /* Reserved */
656         p_sys->p_page->p_region_defs[i].i_x = bs_read( s, 16 );
657         p_sys->p_page->p_region_defs[i].i_y = bs_read( s, 16 );
658
659 #ifdef DEBUG_DVBSUB
660         msg_Dbg( p_dec, "page_composition, region %i (%i,%i)",
661                  i, p_sys->p_page->p_region_defs[i].i_x,
662                  p_sys->p_page->p_region_defs[i].i_y );
663 #endif
664     }
665 }
666
667 static void decode_region_composition( decoder_t *p_dec, bs_t *s )
668 {
669     decoder_sys_t *p_sys = p_dec->p_sys;
670     dvbsub_region_t *p_region, **pp_region = &p_sys->p_regions;
671     int i_segment_length, i_processed_length, i_id, i_version;
672     int i_width, i_height, i_level_comp, i_depth, i_clut;
673     int i_8_bg, i_4_bg, i_2_bg;
674     vlc_bool_t b_fill;
675
676     i_segment_length = bs_read( s, 16 );
677     i_id = bs_read( s, 8 );
678     i_version = bs_read( s, 4 );
679
680     /* Check if we already have this region */
681     for( p_region = p_sys->p_regions; p_region != NULL;
682          p_region = p_region->p_next )
683     {
684         pp_region = &p_region->p_next;
685         if( p_region->i_id == i_id ) break;
686     }
687
688     /* Check version number */
689     if( p_region && p_region->i_version == i_version )
690     {
691         bs_skip( s, 8 * (i_segment_length - 1) - 4 );
692         return;
693     }
694
695     if( !p_region )
696     {
697 #ifdef DEBUG_DVBSUB
698         msg_Dbg( p_dec, "new region: %i", i_id );
699 #endif
700         p_region = *pp_region = malloc( sizeof(dvbsub_region_t) );
701         memset( p_region, 0, sizeof(dvbsub_region_t) );
702         p_region->p_object_defs = NULL;
703         p_region->p_pixbuf = NULL;
704         p_region->p_next = NULL;
705     }
706
707     /* Region attributes */
708     p_region->i_id = i_id;
709     p_region->i_version = i_version;
710     b_fill = bs_read( s, 1 );
711     bs_skip( s, 3 ); /* Reserved */
712
713     i_width = bs_read( s, 16 );
714     i_height = bs_read( s, 16 );
715     i_level_comp = bs_read( s, 3 );
716     i_depth = bs_read( s, 3 );
717     bs_skip( s, 2 ); /* Reserved */
718     i_clut = bs_read( s, 8 );
719
720     i_8_bg = bs_read( s, 8 );
721     i_4_bg = bs_read( s, 4 );
722     i_2_bg = bs_read( s, 2 );
723     bs_skip( s, 2 ); /* Reserved */
724     p_region->i_object_defs    = 0;
725
726     /* Extra sanity checks */
727     if( p_region->i_width != i_width || p_region->i_height != i_height )
728     {
729         if( p_region->p_pixbuf )
730         {
731             msg_Dbg( p_dec, "region size changed (not allowed)" );
732             free( p_region->p_pixbuf );
733         }
734
735         p_region->p_pixbuf = malloc( i_height * i_width );
736         p_region->i_depth = 0;
737         b_fill = VLC_TRUE;
738     }
739     if( p_region->i_depth && (p_region->i_depth != i_depth ||
740         p_region->i_level_comp != i_level_comp || p_region->i_clut != i_clut) )
741     {
742         msg_Dbg( p_dec, "region parameters changed (not allowed)" );
743     }
744
745     /* Erase background of region */
746     if( b_fill )
747     {
748         int i_background = (p_region->i_depth == 1) ? i_2_bg :
749             (p_region->i_depth == 2) ? i_4_bg : i_8_bg;
750         memset( p_region->p_pixbuf, i_background, i_width * i_height );
751     }
752
753     p_region->i_width = i_width;
754     p_region->i_height = i_height;
755     p_region->i_level_comp = i_level_comp;
756     p_region->i_depth = i_depth;
757     p_region->i_clut = i_clut;
758
759     /* List of objects in the region */
760     i_processed_length = 10;
761     while( i_processed_length < i_segment_length )
762     {
763         dvbsub_objectdef_t *p_obj;
764
765         /* We create a new object */
766         p_region->i_object_defs++;
767         p_region->p_object_defs =
768             realloc( p_region->p_object_defs,
769                      sizeof(dvbsub_objectdef_t) * p_region->i_object_defs );
770
771         /* We parse object properties */
772         p_obj = &p_region->p_object_defs[p_region->i_object_defs - 1];
773         p_obj->i_id         = bs_read( s, 16 );
774         p_obj->i_type       = bs_read( s, 2 );
775         bs_skip( s, 2 ); /* Provider */
776         p_obj->i_x          = bs_read( s, 12 );
777         bs_skip( s, 4 ); /* Reserved */
778         p_obj->i_y          = bs_read( s, 12 );
779
780         i_processed_length += 6;
781
782         if( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ||
783             p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING )
784         {
785             p_obj->i_fg_pc =  bs_read( s, 8 );
786             p_obj->i_bg_pc =  bs_read( s, 8 );
787             i_processed_length += 2;
788         }
789     }
790 }
791
792 static void dvbsub_render_pdata( decoder_t *, dvbsub_region_t *, int, int,
793                                  uint8_t *, int );
794 static void dvbsub_pdata2bpp( bs_t *, uint8_t *, int, int * );
795 static void dvbsub_pdata4bpp( bs_t *, uint8_t *, int, int * );
796 static void dvbsub_pdata8bpp( bs_t *, uint8_t *, int, int * );
797
798 static void decode_object( decoder_t *p_dec, bs_t *s )
799 {
800     decoder_sys_t *p_sys = p_dec->p_sys;
801     dvbsub_region_t *p_region;
802     int i_segment_length, i_coding_method, i_version, i_id, i;
803     vlc_bool_t b_non_modify_color;
804
805     i_segment_length = bs_read( s, 16 );
806     i_id             = bs_read( s, 16 );
807     i_version        = bs_read( s, 4 );
808     i_coding_method  = bs_read( s, 2 );
809
810     if( i_coding_method )
811     {
812         /* TODO: DVB subtitling as characters */
813         msg_Dbg( p_dec, "DVB subtitling as characters is not handled!" );
814         bs_skip( s, 8 * (i_segment_length - 2) - 6 );
815         return;
816     }
817
818     /* Check if the object needs to be rendered in at least one
819      * of the regions */
820     for( p_region = p_sys->p_regions; p_region != NULL;
821          p_region = p_region->p_next )
822     {
823         for( i = 0; i < p_region->i_object_defs; i++ )
824             if( p_region->p_object_defs[i].i_id == i_id ) break;
825
826         if( i != p_region->i_object_defs ) break;
827     }
828     if( !p_region )
829     {
830         bs_skip( s, 8 * (i_segment_length - 2) - 6 );
831         return;
832     }
833
834 #ifdef DEBUG_DVBSUB
835     msg_Dbg( p_dec, "new object: %i", i_id );
836 #endif
837
838     b_non_modify_color = bs_read( s, 1 );
839     bs_skip( s, 1 ); /* Reserved */
840
841     if( i_coding_method == 0x00 )
842     {
843         int i_topfield, i_bottomfield;
844         uint8_t *p_topfield, *p_bottomfield;
845
846         i_topfield    = bs_read( s, 16 );
847         i_bottomfield = bs_read( s, 16 );
848         p_topfield    = s->p_start + bs_pos( s ) / 8;
849         p_bottomfield = p_topfield + i_topfield;
850
851         bs_skip( s, 8 * (i_segment_length - 7) );
852
853         /* Sanity check */
854         if( i_segment_length < i_topfield + i_bottomfield + 7 ||
855             p_topfield + i_topfield + i_bottomfield > s->p_end )
856         {
857             msg_Dbg( p_dec, "corrupted object data" );
858             return;
859         }
860
861         for( p_region = p_sys->p_regions; p_region != NULL;
862              p_region = p_region->p_next )
863         {
864             for( i = 0; i < p_region->i_object_defs; i++ )
865             {
866                 if( p_region->p_object_defs[i].i_id != i_id ) continue;
867
868                 dvbsub_render_pdata( p_dec, p_region,
869                                      p_region->p_object_defs[i].i_x,
870                                      p_region->p_object_defs[i].i_y,
871                                      p_topfield, i_topfield );
872
873                 if( i_bottomfield )
874                 {
875                     dvbsub_render_pdata( p_dec, p_region,
876                                          p_region->p_object_defs[i].i_x,
877                                          p_region->p_object_defs[i].i_y + 1,
878                                          p_bottomfield, i_bottomfield );
879                 }
880                 else
881                 {
882                     /* Duplicate the top field */
883                     dvbsub_render_pdata( p_dec, p_region,
884                                          p_region->p_object_defs[i].i_x,
885                                          p_region->p_object_defs[i].i_y + 1,
886                                          p_topfield, i_topfield );
887                 }
888             }
889         }
890     }
891     else
892     {
893         /* TODO: DVB subtitling as characters */
894     }
895
896 #ifdef DEBUG_DVBSUB
897     msg_Dbg( p_dec, "end object: %i", i_id );
898 #endif
899 }
900
901 static void dvbsub_render_pdata( decoder_t *p_dec, dvbsub_region_t *p_region,
902                                  int i_x, int i_y,
903                                  uint8_t *p_field, int i_field )
904 {
905     uint8_t *p_pixbuf;
906     int i_offset = 0;
907     bs_t bs;
908
909     /* Sanity check */
910     if( !p_region->p_pixbuf )
911     {
912         msg_Err( p_dec, "region %i has no pixel buffer!", p_region->i_id );
913         return;
914     }
915     if( i_y < 0 || i_x < 0 || i_y >= p_region->i_height ||
916         i_x >= p_region->i_width )
917     {
918         msg_Dbg( p_dec, "invalid offset (%i,%i)", i_x, i_y );
919         return;
920     }
921
922     p_pixbuf = p_region->p_pixbuf + i_y * p_region->i_width;
923     bs_init( &bs, p_field, i_field );
924
925     while( !bs_eof( &bs ) )
926     {
927         /* Sanity check */
928         if( i_y >= p_region->i_height ) return;
929
930         switch( bs_read( &bs, 8 ) )
931         {
932         case 0x10:
933             dvbsub_pdata2bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
934                               &i_offset );
935             break;
936
937         case 0x11:
938             dvbsub_pdata4bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
939                               &i_offset );
940             break;
941
942         case 0x12:
943             dvbsub_pdata8bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
944                               &i_offset );
945             break;
946
947         case 0x20:
948         case 0x21:
949         case 0x22:
950             /* We don't use map tables */
951             break;
952
953         case 0xf0: /* End of line code */
954             p_pixbuf += 2*p_region->i_width;
955             i_offset = 0; i_y += 2;
956             break;
957         }
958     }
959 }
960
961 static void dvbsub_pdata2bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
962 {
963     vlc_bool_t b_stop = 0;
964
965     while( !b_stop && !bs_eof( s ) )
966     {
967         int i_count = 0, i_color = 0;
968
969         if( (i_color = bs_read( s, 2 )) != 0x00 )
970         {
971             i_count = 1;
972         }
973         else
974         {
975             if( bs_read( s, 1 ) == 0x01 )         // Switch1
976             {
977                 i_count = 3 + bs_read( s, 3 );
978                 i_color = bs_read( s, 2 );
979             }
980             else
981             {
982                 if( bs_read( s, 1 ) == 0x00 )     //Switch2
983                 {
984                     switch( bs_read( s, 2 ) )     //Switch3
985                     {
986                     case 0x00:
987                         b_stop = 1;
988                         break;
989                     case 0x01:
990                         i_count = 2;
991                         break;
992                     case 0x02:
993                         i_count =  12 + bs_read( s, 4 );
994                         i_color = bs_read( s, 2 );
995                         break;
996                     case 0x03:
997                         i_count =  29 + bs_read( s, 8 );
998                         i_color = bs_read( s, 2 );
999                         break;
1000                     default:
1001                         break;
1002                     }
1003                 }
1004                 else
1005                 {
1006                     /* 1 pixel color 0 */
1007                     i_count = 1;
1008                 }
1009             }
1010         }
1011
1012         if( !i_count ) continue;
1013
1014         /* Sanity check */
1015         if( i_count + *pi_off > i_width ) break;
1016
1017         if( i_count == 1 ) p[*pi_off] = i_color;
1018         else memset( p + *pi_off, i_color, i_count );
1019
1020         (*pi_off) += i_count;
1021     }
1022
1023     bs_align( s );
1024 }
1025
1026 static void dvbsub_pdata4bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1027 {
1028     vlc_bool_t b_stop = 0;
1029
1030     while( !b_stop && !bs_eof( s ) )
1031     {
1032         int i_count = 0, i_color = 0;
1033
1034         if( (i_color = bs_read( s, 4 )) != 0x00 )
1035         {
1036             /* Add 1 pixel */
1037             i_count = 1;
1038         }
1039         else
1040         {
1041             if( bs_read( s, 1 ) == 0x00 )           // Switch1
1042             {
1043                 if( bs_show( s, 3 ) != 0x00 )
1044                 {
1045                     i_count = 2 + bs_read( s, 3 );
1046                 }
1047                 else
1048                 {
1049                     bs_skip( s, 3 );
1050                     b_stop = 1;
1051                 }
1052             }
1053             else
1054             {
1055                 if( bs_read( s, 1 ) == 0x00)        //Switch2
1056                 {
1057                     i_count =  4 + bs_read( s, 2 );
1058                     i_color = bs_read( s, 4 );
1059                 }
1060                 else
1061                 {
1062                     switch ( bs_read( s, 2 ) )     //Switch3
1063                     {
1064                     case 0x0:
1065                         i_count = 1;
1066                         break;
1067                     case 0x1:
1068                         i_count = 2;
1069                         break;
1070                     case 0x2:
1071                         i_count = 9 + bs_read( s, 4 );
1072                         i_color = bs_read( s, 4 );
1073                         break;
1074                     case 0x3:
1075                         i_count= 25 + bs_read( s, 8 );
1076                         i_color = bs_read( s, 4 );
1077                         break;
1078                     }
1079                 }
1080             }
1081         }
1082
1083         if( !i_count ) continue;
1084
1085         /* Sanity check */
1086         if( i_count + *pi_off > i_width ) break;
1087
1088         if( i_count == 1 ) p[*pi_off] = i_color;
1089         else memset( p + *pi_off, i_color, i_count );
1090
1091         (*pi_off) += i_count;
1092     }
1093
1094     bs_align( s );
1095 }
1096
1097 static void dvbsub_pdata8bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1098 {
1099     vlc_bool_t b_stop = 0;
1100
1101     while( !b_stop && !bs_eof( s ) )
1102     {
1103         int i_count = 0, i_color = 0;
1104
1105         if( (i_color = bs_read( s, 8 )) != 0x00 )
1106         {
1107             /* Add 1 pixel */
1108             i_count = 1;
1109         }
1110         else
1111         {
1112             if( bs_read( s, 1 ) == 0x00 )           // Switch1
1113             {
1114                 if( bs_show( s, 7 ) != 0x00 )
1115                 {
1116                     i_count = bs_read( s, 7 );
1117                 }
1118                 else
1119                 {
1120                     bs_skip( s, 7 );
1121                     b_stop = 1;
1122                 }
1123             }
1124             else
1125             {
1126                 i_count = bs_read( s, 7 );
1127                 i_color = bs_read( s, 8 );
1128             }
1129         }
1130
1131         if( !i_count ) continue;
1132
1133         /* Sanity check */
1134         if( i_count + *pi_off > i_width ) break;
1135
1136         if( i_count == 1 ) p[*pi_off] = i_color;
1137         else memset( p + *pi_off, i_color, i_count );
1138
1139         (*pi_off) += i_count;
1140     }
1141
1142     bs_align( s );
1143 }
1144
1145 static void free_all( decoder_t *p_dec )
1146 {
1147     decoder_sys_t *p_sys = p_dec->p_sys;
1148     dvbsub_region_t *p_reg, *p_reg_next;
1149     dvbsub_clut_t *p_clut, *p_clut_next;
1150
1151     for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut_next )
1152     {
1153         p_clut_next = p_clut->p_next;
1154         free( p_clut );
1155     }
1156     p_sys->p_cluts = NULL;
1157
1158     for( p_reg = p_sys->p_regions; p_reg != NULL; p_reg = p_reg_next )
1159     {
1160         p_reg_next = p_reg->p_next;
1161         if( p_reg->i_object_defs ) free( p_reg->p_object_defs );
1162         if( p_reg->p_pixbuf ) free( p_reg->p_pixbuf );
1163         free( p_reg );
1164     }
1165     p_sys->p_regions = NULL;
1166
1167     if( p_sys->p_page )
1168     {
1169         if( p_sys->p_page->i_region_defs )
1170             free( p_sys->p_page->p_region_defs );
1171         free( p_sys->p_page );
1172     }
1173     p_sys->p_page = NULL;
1174 }
1175
1176 static subpicture_t *render( decoder_t *p_dec )
1177 {
1178     decoder_sys_t *p_sys = p_dec->p_sys;
1179     subpicture_t *p_spu;
1180     subpicture_region_t **pp_spu_region;
1181     int i, j, i_timeout = 0;
1182
1183     /* Allocate the subpicture internal data. */
1184     p_spu = p_dec->pf_spu_buffer_new( p_dec );
1185     if( !p_spu ) return NULL;
1186
1187     pp_spu_region = &p_spu->p_region;
1188
1189     /* Loop on region definitions */
1190 #ifdef DEBUG_DVBSUB
1191     if( p_sys->p_page )
1192         msg_Dbg( p_dec, "rendering %i regions", p_sys->p_page->i_region_defs );
1193 #endif
1194
1195     for( i = 0; p_sys->p_page && i < p_sys->p_page->i_region_defs; i++ )
1196     {
1197         dvbsub_region_t     *p_region;
1198         dvbsub_regiondef_t  *p_regiondef;
1199         dvbsub_clut_t       *p_clut;
1200         dvbsub_color_t      *p_color;
1201         subpicture_region_t *p_spu_region;
1202         uint8_t *p_src, *p_dst;
1203         video_format_t fmt;
1204         int i_pitch;
1205
1206         i_timeout = p_sys->p_page->i_timeout;
1207
1208         p_regiondef = &p_sys->p_page->p_region_defs[i];
1209
1210 #ifdef DEBUG_DVBSUB
1211         msg_Dbg( p_dec, "rendering region %i (%i,%i)", i,
1212                  p_regiondef->i_x, p_regiondef->i_y );
1213 #endif
1214
1215         /* Find associated region */
1216         for( p_region = p_sys->p_regions; p_region != NULL;
1217              p_region = p_region->p_next )
1218         {
1219             if( p_regiondef->i_id == p_region->i_id ) break;
1220         }
1221
1222         if( !p_region )
1223         {
1224             msg_Dbg( p_dec, "region %i not found", p_regiondef->i_id );
1225             continue;
1226         }
1227
1228         /* Find associated CLUT */
1229         for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next )
1230         {
1231             if( p_region->i_clut == p_clut->i_id ) break;
1232         }
1233         if( !p_clut )
1234         {
1235             msg_Dbg( p_dec, "clut %i not found", p_region->i_clut );
1236             continue;
1237         }
1238
1239         /* Create new SPU region */
1240         memset( &fmt, 0, sizeof(video_format_t) );
1241         fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
1242         fmt.i_aspect = VOUT_ASPECT_FACTOR;
1243         fmt.i_width = fmt.i_visible_width = p_region->i_width;
1244         fmt.i_height = fmt.i_visible_height = p_region->i_height;
1245         fmt.i_x_offset = fmt.i_y_offset = 0;
1246         p_spu_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
1247         if( !p_region )
1248         {
1249             msg_Err( p_dec, "cannot allocate SPU region" );
1250             continue;
1251         }
1252         p_spu_region->i_x = p_regiondef->i_x;
1253         p_spu_region->i_y = p_regiondef->i_y;
1254         *pp_spu_region = p_spu_region;
1255         pp_spu_region = &p_spu_region->p_next;
1256
1257         /* Build palette */
1258         fmt.p_palette->i_entries = p_region->i_depth == 1 ? 4 :
1259             p_region->i_depth == 2 ? 16 : 256;
1260         p_color = (p_region->i_depth == 1) ? p_clut->c_2b :
1261             (p_region->i_depth == 2) ? p_clut->c_4b : p_clut->c_8b;
1262         for( j = 0; j < fmt.p_palette->i_entries; j++ )
1263         {
1264             fmt.p_palette->palette[j][0] = p_color[j].Y;
1265             fmt.p_palette->palette[j][1] = p_color[j].Cr;
1266             fmt.p_palette->palette[j][2] = p_color[j].Cb;
1267             fmt.p_palette->palette[j][3] = 0xff - p_color[j].T;
1268         }
1269
1270         p_src = p_region->p_pixbuf;
1271         p_dst = p_spu_region->picture.Y_PIXELS;
1272         i_pitch = p_spu_region->picture.Y_PITCH;
1273
1274         /* Copy pixel buffer */
1275         for( j = 0; j < p_region->i_height; j++ )
1276         {
1277             memcpy( p_dst, p_src, p_region->i_width );
1278             p_src += p_region->i_width;
1279             p_dst += i_pitch;
1280         }
1281     }
1282
1283     /* Set the pf_render callback */
1284     p_spu->i_start = p_sys->i_pts;
1285     p_spu->i_stop = p_spu->i_start + i_timeout * 1000000;
1286     p_spu->b_ephemer = VLC_TRUE;
1287
1288     return p_spu;
1289 }
1290
1291 /*****************************************************************************
1292  * encoder_sys_t : encoder descriptor
1293  *****************************************************************************/
1294 typedef struct encoder_region_t
1295 {
1296     int i_width;
1297     int i_height;
1298
1299 } encoder_region_t;
1300
1301 struct encoder_sys_t
1302 {
1303     unsigned int i_page_ver;
1304     unsigned int i_region_ver;
1305     unsigned int i_clut_ver;
1306
1307     int i_regions;
1308     encoder_region_t *p_regions;
1309
1310     mtime_t i_pts;
1311 };
1312
1313 static void encode_page_composition( encoder_t *, bs_t *, subpicture_t * );
1314 static void encode_clut( encoder_t *, bs_t *, subpicture_t * );
1315 static void encode_region_composition( encoder_t *, bs_t *, subpicture_t * );
1316 static void encode_object( encoder_t *, bs_t *, subpicture_t * );
1317
1318 /*****************************************************************************
1319  * OpenEncoder: probe the encoder and return score
1320  *****************************************************************************/
1321 static int OpenEncoder( vlc_object_t *p_this )
1322 {
1323     encoder_t *p_enc = (encoder_t *)p_this;
1324     encoder_sys_t *p_sys;
1325
1326     if( p_enc->fmt_out.i_codec != VLC_FOURCC('d','v','b','s') &&
1327         !p_enc->b_force )
1328     {
1329         return VLC_EGENERIC;
1330     }
1331
1332     /* Allocate the memory needed to store the decoder's structure */
1333     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1334     {
1335         msg_Err( p_enc, "out of memory" );
1336         return VLC_EGENERIC;
1337     }
1338     p_enc->p_sys = p_sys;
1339
1340     p_enc->pf_encode_sub = Encode;
1341     p_enc->fmt_out.i_codec = VLC_FOURCC('d','v','b','s');
1342     p_enc->fmt_out.subs.dvb.i_id  = 1 << 16 | 1;
1343
1344     sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
1345
1346     p_sys->i_page_ver = 0;
1347     p_sys->i_region_ver = 0;
1348     p_sys->i_clut_ver = 0;
1349     p_sys->i_regions = 0;
1350     p_sys->p_regions = 0;
1351
1352     return VLC_SUCCESS;
1353 }
1354
1355 /****************************************************************************
1356  * Encode: the whole thing
1357  ****************************************************************************/
1358 static block_t *Encode( encoder_t *p_enc, subpicture_t *p_subpic )
1359 {
1360     bs_t bits, *s = &bits;
1361     block_t *p_block;
1362
1363     if( !p_subpic || !p_subpic->p_region ) return 0;
1364
1365     msg_Dbg( p_enc, "encoding subpicture" );
1366
1367     p_block = block_New( p_enc, 64000 );
1368     bs_init( s, p_block->p_buffer, p_block->i_buffer );
1369
1370     bs_write( s, 8, 0x20 ); /* Data identifier */
1371     bs_write( s, 8, 0x0 ); /* Subtitle stream id */
1372
1373     encode_page_composition( p_enc, s, p_subpic );
1374     encode_region_composition( p_enc, s, p_subpic );
1375     encode_clut( p_enc, s, p_subpic );
1376     encode_object( p_enc, s, p_subpic );
1377
1378     /* End of display */
1379     bs_write( s, 8, 0x0f ); /* Sync byte */
1380     bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
1381     bs_write( s, 16, 1 ); /* Page id */
1382     bs_write( s, 16, 0 ); /* Segment length */
1383
1384     bs_write( s, 8, 0xff ); /* End marker */
1385     p_block->i_buffer = bs_pos( s ) / 8;
1386     p_block->i_pts = p_block->i_dts = p_subpic->i_start;
1387     if( !p_subpic->b_ephemer && p_subpic->i_stop > p_subpic->i_start )
1388     {
1389         block_t *p_block_stop;
1390
1391         p_block->i_length = p_subpic->i_stop - p_subpic->i_start;
1392
1393         /* Send another (empty) subtitle to signal the end of display */
1394         p_block_stop = block_New( p_enc, 64000 );
1395         bs_init( s, p_block_stop->p_buffer, p_block_stop->i_buffer );
1396         bs_write( s, 8, 0x20 ); /* Data identifier */
1397         bs_write( s, 8, 0x0 ); /* Subtitle stream id */
1398         encode_page_composition( p_enc, s, 0 );
1399         bs_write( s, 8, 0x0f ); /* Sync byte */
1400         bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
1401         bs_write( s, 16, 1 ); /* Page id */
1402         bs_write( s, 16, 0 ); /* Segment length */
1403         bs_write( s, 8, 0xff ); /* End marker */
1404         p_block_stop->i_buffer = bs_pos( s ) / 8;
1405         p_block_stop->i_pts = p_block_stop->i_dts = p_subpic->i_stop;
1406         block_ChainAppend( &p_block, p_block_stop );
1407         p_block_stop->i_length = 100000;//p_subpic->i_stop - p_subpic->i_start;
1408     }
1409
1410     msg_Dbg( p_enc, "subpicture encoded properly" );
1411
1412     return p_block;
1413 }
1414
1415 /*****************************************************************************
1416  * CloseEncoder: encoder destruction
1417  *****************************************************************************/
1418 static void CloseEncoder( vlc_object_t *p_this )
1419 {
1420     encoder_t *p_enc = (encoder_t *)p_this;
1421     encoder_sys_t *p_sys = p_enc->p_sys;
1422
1423     if( p_sys->i_regions ) free( p_sys->p_regions );
1424     free( p_sys );
1425 }
1426
1427 static void encode_page_composition( encoder_t *p_enc, bs_t *s,
1428                                      subpicture_t *p_subpic )
1429 {
1430     encoder_sys_t *p_sys = p_enc->p_sys;
1431     subpicture_region_t *p_region;
1432     vlc_bool_t b_mode_change = VLC_FALSE;
1433     int i_regions, i_timeout;
1434
1435     bs_write( s, 8, 0x0f ); /* Sync byte */
1436     bs_write( s, 8, DVBSUB_ST_PAGE_COMPOSITION ); /* Segment type */
1437     bs_write( s, 16, 1 ); /* Page id */
1438
1439     for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
1440          p_region; p_region = p_region->p_next, i_regions++ )
1441     {
1442         if( i_regions >= p_sys->i_regions )
1443         {
1444             encoder_region_t region;
1445             region.i_width = region.i_height = 0;
1446             p_sys->p_regions =
1447                 realloc( p_sys->p_regions, sizeof(encoder_region_t) *
1448                          (p_sys->i_regions + 1) );
1449             p_sys->p_regions[p_sys->i_regions++] = region;
1450         }
1451
1452         if( p_sys->p_regions[i_regions].i_width <
1453             (int)p_region->fmt.i_visible_width )
1454         {
1455             b_mode_change = VLC_TRUE;
1456             msg_Dbg( p_enc, "region %i width change: %i -> %i",
1457                      i_regions, p_sys->p_regions[i_regions].i_width,
1458                      p_region->fmt.i_visible_width );
1459             p_sys->p_regions[i_regions].i_width =
1460                 p_region->fmt.i_visible_width;
1461         }
1462         if( p_sys->p_regions[i_regions].i_height <
1463             (int)p_region->fmt.i_visible_height )
1464         {
1465             b_mode_change = VLC_TRUE;
1466             msg_Dbg( p_enc, "region %i height change: %i -> %i",
1467                      i_regions, p_sys->p_regions[i_regions].i_height,
1468                      p_region->fmt.i_visible_height );
1469             p_sys->p_regions[i_regions].i_height =
1470                 p_region->fmt.i_visible_height;
1471         }
1472     }
1473
1474     bs_write( s, 16, i_regions * 6 + 2 ); /* Segment length */
1475
1476     i_timeout = 0;
1477     if( p_subpic && !p_subpic->b_ephemer &&
1478         p_subpic->i_stop > p_subpic->i_start )
1479     {
1480         i_timeout = (p_subpic->i_stop - p_subpic->i_start) / 1000000;
1481     }
1482
1483     bs_write( s, 8, i_timeout + 15 ); /* Timeout */
1484     bs_write( s, 4, p_sys->i_page_ver++ );
1485     bs_write( s, 2, b_mode_change ?
1486               DVBSUB_PCS_STATE_CHANGE : DVBSUB_PCS_STATE_ACQUISITION );
1487     bs_write( s, 2, 0 ); /* Reserved */
1488
1489     for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
1490          p_region; p_region = p_region->p_next, i_regions++ )
1491     {
1492         bs_write( s, 8, i_regions );
1493         bs_write( s, 8, 0 ); /* Reserved */
1494         bs_write( s, 16, p_region->i_x );
1495         bs_write( s, 16, p_region->i_y );
1496     }
1497 }
1498
1499 static void encode_clut( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
1500 {
1501     encoder_sys_t *p_sys = p_enc->p_sys;
1502     subpicture_region_t *p_region = p_subpic->p_region;
1503     video_palette_t *p_pal;
1504     int i;
1505
1506     /* Sanity check */
1507     if( !p_region || !p_region->fmt.p_palette ||
1508         p_region->fmt.i_chroma != VLC_FOURCC('Y','U','V','P') ) return;
1509
1510     bs_write( s, 8, 0x0f ); /* Sync byte */
1511     bs_write( s, 8, DVBSUB_ST_CLUT_DEFINITION ); /* Segment type */
1512     bs_write( s, 16, 1 ); /* Page id */
1513
1514     p_pal = p_region->fmt.p_palette;
1515
1516     bs_write( s, 16, p_pal->i_entries * 6 + 2 ); /* Segment length */
1517     bs_write( s, 8, 1 ); /* Clut id */
1518     bs_write( s, 4, p_sys->i_clut_ver++ );
1519     bs_write( s, 4, 0 ); /* Reserved */
1520
1521     for( i = 0; i < p_pal->i_entries; i++ )
1522     {
1523         bs_write( s, 8, i ); /* Clut entry id */
1524         bs_write( s, 1, p_pal->i_entries == 4 );   /* 2bit/entry flag */
1525         bs_write( s, 1, p_pal->i_entries == 16 );  /* 4bit/entry flag */
1526         bs_write( s, 1, p_pal->i_entries == 256 ); /* 8bit/entry flag */
1527         bs_write( s, 4, 0 ); /* Reserved */
1528         bs_write( s, 1, 1 ); /* Full range flag */
1529         bs_write( s, 8, p_pal->palette[i][3] ?  /* Y value */
1530                   (p_pal->palette[i][0] ? p_pal->palette[i][0] : 16) : 0 );
1531         bs_write( s, 8, p_pal->palette[i][1] ); /* Cr value */
1532         bs_write( s, 8, p_pal->palette[i][2] ); /* Cb value */
1533         bs_write( s, 8, 0xff - p_pal->palette[i][3] ); /* T value */
1534     }
1535 }
1536
1537 static void encode_region_composition( encoder_t *p_enc, bs_t *s,
1538                                        subpicture_t *p_subpic )
1539 {
1540     encoder_sys_t *p_sys = p_enc->p_sys;
1541     subpicture_region_t *p_region;
1542     int i_region, i_bg;
1543
1544     for( i_region = 0, p_region = p_subpic->p_region; p_region;
1545          p_region = p_region->p_next, i_region++ )
1546     {
1547         video_palette_t *p_pal = p_region->fmt.p_palette;
1548         int i_depth = p_pal->i_entries == 4 ? 0x1 :
1549             p_pal->i_entries == 16 ? 0x2 : 0x3;
1550
1551         for( i_bg = 0; i_bg < p_pal->i_entries; i_bg++ )
1552         {
1553             if( !p_pal->palette[i_bg][3] ) break;
1554         }
1555
1556         bs_write( s, 8, 0x0f ); /* Sync byte */
1557         bs_write( s, 8, DVBSUB_ST_REGION_COMPOSITION ); /* Segment type */
1558         bs_write( s, 16, 1 ); /* Page id */
1559
1560         bs_write( s, 16, 10 + 6 ); /* Segment length */
1561         bs_write( s, 8, i_region );
1562         bs_write( s, 4, p_sys->i_region_ver++ );
1563
1564         /* Region attributes */
1565         bs_write( s, 1, i_bg < p_pal->i_entries ); /* Fill */
1566         bs_write( s, 3, 0 ); /* Reserved */
1567         bs_write( s, 16, p_sys->p_regions[i_region].i_width );
1568         bs_write( s, 16, p_sys->p_regions[i_region].i_height );
1569         bs_write( s, 3, i_depth );  /* Region level of compatibility */
1570         bs_write( s, 3, i_depth  ); /* Region depth */
1571         bs_write( s, 2, 0 ); /* Reserved */
1572         bs_write( s, 8, 1 ); /* Clut id */
1573         bs_write( s, 8, i_bg ); /* region 8bit pixel code */
1574         bs_write( s, 4, i_bg ); /* region 4bit pixel code */
1575         bs_write( s, 2, i_bg ); /* region 2bit pixel code */
1576         bs_write( s, 2, 0 ); /* Reserved */
1577
1578         /* In our implementation we only have 1 object per region */
1579         bs_write( s, 16, i_region );
1580         bs_write( s, 2, DVBSUB_OT_BASIC_BITMAP );
1581         bs_write( s, 2, 0 ); /* object provider flag */
1582         bs_write( s, 12, 0 ); /* object horizontal position */
1583         bs_write( s, 4, 0 ); /* Reserved */
1584         bs_write( s, 12, 0 ); /* object vertical position */
1585     }
1586 }
1587
1588 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
1589                                subpicture_region_t *p_region,
1590                                vlc_bool_t b_top );
1591
1592 static void encode_object( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
1593 {
1594     encoder_sys_t   *p_sys = p_enc->p_sys;
1595     subpicture_region_t *p_region;
1596     int i_region;
1597
1598     int i_length_pos, i_update_pos, i_pixel_data_pos;
1599
1600     for( i_region = 0, p_region = p_subpic->p_region; p_region;
1601          p_region = p_region->p_next, i_region++ )
1602     {
1603         bs_write( s, 8, 0x0f ); /* Sync byte */
1604         bs_write( s, 8, DVBSUB_ST_OBJECT_DATA ); /* Segment type */
1605         bs_write( s, 16, 1 ); /* Page id */
1606
1607         i_length_pos = bs_pos( s );
1608         bs_write( s, 16, 0 ); /* Segment length */
1609         bs_write( s, 16, i_region ); /* Object id */
1610         bs_write( s, 4, p_sys->i_region_ver++ );
1611         bs_write( s, 2, 0 ); /* object coding method */
1612
1613         bs_write( s, 1, 0 ); /* non modifying color flag */
1614         bs_write( s, 1, 0 ); /* Reserved */
1615
1616         i_update_pos = bs_pos( s );
1617         bs_write( s, 16, 0 ); /* topfield data block length */
1618         bs_write( s, 16, 0 ); /* bottomfield data block length */
1619
1620         /* Top field */
1621         i_pixel_data_pos = bs_pos( s );
1622         encode_pixel_data( p_enc, s, p_region, VLC_TRUE );
1623         i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
1624         SetWBE( &s->p_start[i_update_pos/8], i_pixel_data_pos );
1625
1626         /* Bottom field */
1627         i_pixel_data_pos = bs_pos( s );
1628         encode_pixel_data( p_enc, s, p_region, VLC_FALSE );
1629         i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
1630         SetWBE( &s->p_start[i_update_pos/8+2], i_pixel_data_pos );
1631
1632         /* Stuffing for word alignment */
1633         bs_align_0( s );
1634         if( bs_pos( s ) % 16 ) bs_write( s, 8, 0 );
1635
1636         /* Update segment length */
1637         SetWBE( &s->p_start[i_length_pos/8], (bs_pos(s) - i_length_pos)/8 -2 );
1638     }
1639 }
1640
1641 static void encode_pixel_line_2bp( encoder_t *p_enc, bs_t *s,
1642                                    subpicture_region_t *p_region,
1643                                    int i_line );
1644 static void encode_pixel_line_4bp( encoder_t *p_enc, bs_t *s,
1645                                    subpicture_region_t *p_region,
1646                                    int i_line );
1647 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
1648                                subpicture_region_t *p_region,
1649                                vlc_bool_t b_top )
1650 {
1651     unsigned int i_line;
1652
1653     /* Sanity check */
1654     if( p_region->fmt.i_chroma != VLC_FOURCC('Y','U','V','P') ) return;
1655
1656     /* Encode line by line */
1657     for( i_line = !b_top; i_line < p_region->fmt.i_visible_height;
1658          i_line += 2 )
1659     {
1660         switch( p_region->fmt.p_palette->i_entries )
1661         {
1662         case 4:
1663             bs_write( s, 8, 0x10 ); /* 2 bit/pixel code string */
1664             encode_pixel_line_2bp( p_enc, s, p_region, i_line );
1665             break;
1666
1667         case 16:
1668             bs_write( s, 8, 0x11 ); /* 4 bit/pixel code string */
1669             encode_pixel_line_4bp( p_enc, s, p_region, i_line );
1670             break;
1671
1672         default:
1673             msg_Err( p_enc, "subpicture palette (%i) not handled",
1674                      p_region->fmt.p_palette->i_entries );
1675             break;
1676         }
1677
1678         bs_write( s, 8, 0xf0 ); /* End of object line code */
1679     }
1680 }
1681
1682 static void encode_pixel_line_2bp( encoder_t *p_enc, bs_t *s,
1683                                    subpicture_region_t *p_region,
1684                                    int i_line )
1685 {
1686     unsigned int i, i_length = 0;
1687     int i_pitch = p_region->picture.p->i_pitch;
1688     uint8_t *p_data = &p_region->picture.p->p_pixels[ i_pitch * i_line ];
1689     int i_last_pixel = p_data[0];
1690
1691     for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
1692     {
1693         if( i != p_region->fmt.i_visible_width &&
1694             p_data[i] == i_last_pixel && i_length != 284 )
1695         {
1696             i_length++;
1697             continue;
1698         }
1699
1700         if( i_length == 1 || i_length == 11 || i_length == 28 )
1701         {
1702             /* 2bit/pixel code */
1703             if( i_last_pixel ) bs_write( s, 2, i_last_pixel );
1704             else
1705             {
1706                 bs_write( s, 2, 0 );
1707                 bs_write( s, 1, 0 );
1708                 bs_write( s, 1, 1 ); /* pseudo color 0 */
1709             }
1710             i_length--;
1711         }
1712
1713         if( i_length == 2 )
1714         {
1715             if( i_last_pixel )
1716             {
1717                 bs_write( s, 2, i_last_pixel );
1718                 bs_write( s, 2, i_last_pixel );
1719             }
1720             else
1721             {
1722                 bs_write( s, 2, 0 );
1723                 bs_write( s, 1, 0 );
1724                 bs_write( s, 1, 0 );
1725                 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
1726             }
1727         }
1728         else if( i_length > 2 )
1729         {
1730             bs_write( s, 2, 0 );
1731             if( i_length <= 10 )
1732             {
1733                 bs_write( s, 1, 1 );
1734                 bs_write( s, 3, i_length - 3 );
1735                 bs_write( s, 2, i_last_pixel );
1736             }
1737             else
1738             {
1739                 bs_write( s, 1, 0 );
1740                 bs_write( s, 1, 0 );
1741
1742                 if( i_length <= 27 )
1743                 {
1744                     bs_write( s, 2, 2 );
1745                     bs_write( s, 4, i_length - 12 );
1746                     bs_write( s, 2, i_last_pixel );
1747                 }
1748                 else
1749                 {
1750                     bs_write( s, 2, 3 );
1751                     bs_write( s, 8, i_length - 29 );
1752                     bs_write( s, 2, i_last_pixel );
1753                 }
1754             }
1755         }
1756
1757         if( i == p_region->fmt.i_visible_width ) break;
1758
1759         i_last_pixel = p_data[i];
1760         i_length = 1;
1761     }
1762
1763     /* Stop */
1764     bs_write( s, 2, 0 );
1765     bs_write( s, 1, 0 );
1766     bs_write( s, 1, 0 );
1767     bs_write( s, 2, 0 );
1768
1769     /* Stuffing */
1770     bs_align_0( s );
1771 }
1772
1773 static void encode_pixel_line_4bp( encoder_t *p_enc, bs_t *s,
1774                                    subpicture_region_t *p_region,
1775                                    int i_line )
1776 {
1777     unsigned int i, i_length = 0;
1778     int i_pitch = p_region->picture.p->i_pitch;
1779     uint8_t *p_data = &p_region->picture.p->p_pixels[ i_pitch * i_line ];
1780     int i_last_pixel = p_data[0];
1781
1782     for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
1783     {
1784         if( i != p_region->fmt.i_visible_width &&
1785             p_data[i] == i_last_pixel && i_length != 1 )
1786         {
1787             i_length++;
1788             continue;
1789         }
1790
1791         if( i_length == 1 )
1792         {
1793             /* 4bit/pixel code */
1794             if( i_last_pixel ) bs_write( s, 4, i_last_pixel );
1795             else
1796             {
1797                 bs_write( s, 4, 0 );
1798                 bs_write( s, 1, 1 );
1799                 bs_write( s, 1, 1 );
1800                 bs_write( s, 2, 0 ); /* pseudo color 0 */
1801             }
1802         }
1803
1804         if( i == p_region->fmt.i_visible_width ) break;
1805
1806         i_last_pixel = p_data[i];
1807         i_length = 1;
1808     }
1809
1810     /* Stop */
1811     bs_write( s, 8, 0 );
1812
1813     /* Stuffing */
1814     bs_align_0( s );
1815 }