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