]> git.sesse.net Git - vlc/blob - modules/codec/dvbsub.c
* ALL: removed useless add_category_hint().
[vlc] / modules / codec / dvbsub.c
1 /*****************************************************************************
2  * dvbsub.c : DVB subtitles decoder thread
3  *****************************************************************************
4  * Copyright (C) 2003 ANEVIA
5  * Copyright (C) 2003 VideoLAN
6  * $Id: dvbsub.c,v 1.9 2004/01/25 20:40:59 gbazin Exp $
7  *
8  * Authors: Damien LUCAS <damien.lucas@anevia.com>
9  *          Laurent Aimar <fenrir@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc/vout.h>
30 #include <vlc/decoder.h>
31
32 #include "vlc_bits.h"
33
34 /*****************************************************************************
35  * Module descriptor.
36  *****************************************************************************/
37 static int  Open ( vlc_object_t *p_this );
38 static void Close( vlc_object_t *p_this );
39
40 vlc_module_begin();
41     set_description( _("DVB subtitles decoder") );
42     set_capability( "decoder", 50 );
43     set_callbacks( Open, Close );
44 vlc_module_end();
45
46
47 // Wow, that's ugly but very usefull for a memory leak track
48 // so I just keep it
49 #if 0
50 static long long unsigned int trox_malloc_nb = 0;
51 static long long unsigned int trox_free_nb = 0;
52
53 static void* trox_malloc (size_t size)
54 { ++trox_malloc_nb; return malloc (size); }
55
56 static void trox_free (void* ptr)
57 { ++trox_free_nb; free(ptr); return; }
58
59 static void trox_call ()
60 {
61   fprintf(stderr, "dvbbsub -- Memory usage:  %llu mallocs %llu frees (%llu)\n",
62                   trox_malloc_nb,
63                   trox_free_nb,
64                   trox_malloc_nb - trox_free_nb);
65   return;
66 }
67 #else
68 # define trox_malloc malloc
69 # define trox_free free
70 # define trox_call()
71 #endif
72 /****************************************************************************
73  * Local structures
74  ****************************************************************************
75  * Those structures refer closely to the ETSI 300 743 Object model
76  ****************************************************************************/
77
78 /* Storage of a RLE entry */
79 typedef struct dvbsub_rle_s
80 {
81     uint16_t                 i_num;
82     uint8_t                 i_color_code;
83     uint8_t                 y;
84     uint8_t                 cr;
85     uint8_t                 cb;
86     uint8_t                 t;
87     struct dvbsub_rle_s*    p_next;
88 } dvbsub_rle_t;
89
90 /* A subpicture image is a list of codes
91  * We need to store the length of each line since nothing specify in
92  * the standard that all lines shoudl have the same length
93  * WARNING: We assume here that a spu is less than 576 lines high */
94 typedef struct
95 {
96     uint16_t                        i_rows;
97     uint16_t                        i_cols[576];
98     dvbsub_rle_t*                   p_last;
99     dvbsub_rle_t*                   p_codes;
100 } dvbsub_image_t;
101
102 /* The object definition gives the position of the object in a region */
103 typedef struct dvbsub_objectdef_s
104 {
105     uint16_t                    i_id;
106     uint8_t                     i_type;
107     uint8_t                     i_provider;
108     uint16_t                    i_xoffset;
109     uint16_t                    i_yoffset;
110     uint8_t                     i_fg_pc;
111     uint8_t                     i_bg_pc;
112     struct dvbsub_objectdef_s*  p_next;
113 } dvbsub_objectdef_t;
114
115 /* The Region is an aera on the image
116  * with a list of the object definitions associated
117  * and a CLUT */
118 typedef struct dvbsub_region_s
119 {
120     uint8_t                 i_id;
121     uint8_t                 i_version_number;
122     vlc_bool_t              b_fill;
123     uint16_t                i_x;
124     uint16_t                i_y;
125     uint16_t                i_width;
126     uint16_t                i_height;
127     uint8_t                 i_level_comp;
128     uint8_t                 i_depth;
129     uint8_t                 i_clut;
130     uint8_t                 i_8bp_code;
131     uint8_t                 i_4bp_code;
132     uint8_t                 i_2bp_code;
133     dvbsub_objectdef_t*    p_object;
134 } dvbsub_region_t;
135
136 /* The page defines the list of regions */
137 typedef struct
138 {
139     uint16_t              i_id;
140     uint8_t               i_timeout;
141     uint8_t               i_state;
142     uint8_t               i_version_number;
143     uint8_t               i_regions_number;
144     dvbsub_region_t*      regions;
145 } dvbsub_page_t;
146
147 /* An object is constituted of 2 images (for interleaving) */
148 typedef struct dvbsub_object_s
149 {
150     uint16_t                i_id;
151     uint8_t                 i_version_number;
152     uint8_t                 i_coding_method;
153     vlc_bool_t              b_non_modify_color;
154     dvbsub_image_t*         topfield;
155     dvbsub_image_t*         bottomfield;
156     struct dvbsub_object_s* p_next;
157 } dvbsub_object_t;
158
159 /* The entry in the palette CLUT */
160 typedef struct
161 {
162     uint8_t                 Y;
163     uint8_t                 Cr;
164     uint8_t                 Cb;
165     uint8_t                 T;
166 } dvbsub_color_t;
167
168 /* */
169 typedef struct
170 {
171     uint8_t                 i_id;
172     uint8_t                 i_version_number;
173     dvbsub_color_t          c_2b[0xff];
174     dvbsub_color_t          c_4b[0xff];
175     dvbsub_color_t          c_8b[0xff];
176 } dvbsub_clut_t;
177
178 typedef struct
179 {
180     uint8_t                 i_x;
181     uint16_t                i_y;
182     dvbsub_image_t*         p_rle_top;
183     dvbsub_image_t*         p_rle_bot;
184 } dvbsub_render_t;
185
186 typedef struct
187 {
188     int i_id;
189
190     mtime_t i_pts;
191
192     dvbsub_clut_t*          p_clut[0xff];
193     dvbsub_page_t*          p_page;
194     dvbsub_object_t*        p_objects;
195     subpicture_t*           p_spu[16];
196 } dvbsub_all_t;
197
198 struct subpicture_sys_t
199 {
200     mtime_t         i_pts;
201     void *          p_data;                          /* rle datas are stored */
202     vlc_object_t*   p_input;                            /* Link to the input */
203     vlc_bool_t      b_obsolete;
204 };
205
206 struct decoder_sys_t
207 {
208     vout_thread_t *p_vout;
209
210     bs_t          bs;
211
212     dvbsub_all_t dvbsub;
213 };
214
215
216 // List of different SEGMENT TYPES
217 // According to EN 300-743, table 2
218 #define DVBSUB_ST_PAGE_COMPOSITION      0x10
219 #define DVBSUB_ST_REGION_COMPOSITION    0x11
220 #define DVBSUB_ST_CLUT_DEFINITION       0x12
221 #define DVBSUB_ST_OBJECT_DATA           0x13
222 #define DVBSUB_ST_ENDOFDISPLAY          0x80
223 #define DVBSUB_ST_STUFFING              0xff
224 // List of different OBJECT TYPES
225 // According to EN 300-743, table 6
226 #define DVBSUB_OT_BASIC_BITMAP          0x00
227 #define DVBSUB_OT_BASIC_CHAR            0x01
228 #define DVBSUB_OT_COMPOSITE_STRING      0x02
229 // Pixel DATA TYPES
230 // According to EN 300-743, table 9
231 #define DVBSUB_DT_2BP_CODE_STRING       0x10
232 #define DVBSUB_DT_4BP_CODE_STRING       0x11
233 #define DVBSUB_DT_8BP_CODE_STRING       0x12
234 #define DVBSUB_DT_24_TABLE_DATA         0x20
235 #define DVBSUB_DT_28_TABLE_DATA         0x21
236 #define DVBSUB_DT_48_TABLE_DATA         0x22
237 #define DVBSUB_DT_END_LINE              0xf0
238
239 /*****************************************************************************
240  * Local prototypes
241  *****************************************************************************/
242 static void Decode   ( decoder_t *, block_t ** );
243
244 static vout_thread_t *FindVout( decoder_t * );
245
246 static int  dvbsub_init( dvbsub_all_t *, int  );
247 static void dvbsub_decode_segment( dvbsub_all_t *p_dvbsub, bs_t *s );
248 static void dvbsub_render( dvbsub_all_t *, vout_thread_t * );
249 static void dvbsub_clean( dvbsub_all_t * );
250
251
252 /*****************************************************************************
253  * Open: probe the decoder and return score
254  *****************************************************************************
255  * Tries to launch a decoder and return score so that the interface is able
256  * to chose.
257  *****************************************************************************/
258 static int Open( vlc_object_t *p_this )
259 {
260     decoder_t     *p_dec = (decoder_t*) p_this;
261     decoder_sys_t *p_sys;
262
263     if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','v','b','s') )
264     {
265         return VLC_EGENERIC;
266     }
267
268     p_dec->pf_decode_sub = Decode;
269     p_sys = p_dec->p_sys = malloc( sizeof( decoder_sys_t ) );
270
271     p_sys->p_vout = NULL;
272
273     dvbsub_init( &p_sys->dvbsub, p_dec->fmt_in.subs.dvb.i_id );
274
275     es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'd','v','b','s' ) );
276
277     return VLC_SUCCESS;
278 }
279
280 /*****************************************************************************
281  * Close:
282  *****************************************************************************/
283 static void Close( vlc_object_t *p_this )
284 {
285     decoder_t     *p_dec = (decoder_t*) p_this;
286     decoder_sys_t *p_sys = p_dec->p_sys;
287
288     if( p_sys->p_vout && p_sys->p_vout->p_subpicture != NULL )
289     {
290         subpicture_t *  p_subpic;
291         int i_subpic;
292         for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
293         {
294             p_subpic = &p_sys->p_vout->p_subpicture[i_subpic];
295             if( p_subpic != NULL &&
296               ( ( p_subpic->i_status == RESERVED_SUBPICTURE ) ||
297                 ( p_subpic->i_status == READY_SUBPICTURE ) ) )
298             {
299                 vout_DestroySubPicture( p_sys->p_vout, p_subpic );
300             }
301         }
302     }
303
304     dvbsub_clean( &p_sys->dvbsub );
305
306     free( p_sys );
307 }
308
309 /*****************************************************************************
310  * Decode:
311  *****************************************************************************/
312 static void Decode( decoder_t *p_dec, block_t **pp_block )
313 {
314     decoder_sys_t *p_sys = p_dec->p_sys;
315     block_t       *p_block;
316
317     if( pp_block == NULL || *pp_block == NULL )
318     {
319         return;
320     }
321     p_block = *pp_block;
322     *pp_block = NULL;
323
324     p_sys->dvbsub.i_pts = p_block->i_pts;
325     if( p_sys->dvbsub.i_pts <= 0 )
326     {
327         msg_Warn( p_dec, "non dated subtitle" );
328         block_Release( p_block );
329         return;
330     }
331
332     if( ( p_sys->p_vout = FindVout( p_dec ) ) )
333     {
334         int i_data_identifier;
335         int i_subtitle_stream_id;
336         int i_end_data_marker;
337
338         bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
339
340         i_data_identifier = bs_read( &p_sys->bs, 8 );
341         i_subtitle_stream_id = bs_read( &p_sys->bs, 8 );
342
343         for( ;; )
344         {
345             if( bs_show( &p_sys->bs, 8 ) != 0x0f )
346             {
347                 break;
348             }
349             dvbsub_decode_segment( &p_sys->dvbsub, &p_sys->bs );
350         }
351         i_end_data_marker = bs_read( &p_sys->bs, 8 );
352
353         /* Check if the page is to be displayed */
354         if( p_sys->dvbsub.p_page && p_sys->dvbsub.p_objects )
355         {
356             dvbsub_render( &p_sys->dvbsub, p_sys->p_vout );
357         }
358
359         vlc_object_release( p_sys->p_vout );
360     }
361
362     block_Release( p_block );
363 }
364
365 /* following functions are local */
366 /*****************************************************************************
367  * FindVout: Find a vout or wait for one to be created.
368  *****************************************************************************/
369 static vout_thread_t *FindVout( decoder_t *p_dec )
370 {
371     for( ;; )
372     {
373         vout_thread_t *p_vout;
374
375         if( p_dec->b_die || p_dec->b_error )
376         {
377             return NULL;
378         }
379         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
380         if( p_vout )
381         {
382             return p_vout;
383         }
384         msleep( VOUT_OUTMEM_SLEEP );
385     }
386 }
387
388
389 static int dvbsub_init( dvbsub_all_t *p_dvbsub, int i_id )
390 {
391     int i;
392
393     memset( p_dvbsub, 0, sizeof( dvbsub_all_t ) );
394
395     p_dvbsub->i_pts     = 0;
396     p_dvbsub->i_id      = i_id;
397     p_dvbsub->p_page     = NULL;
398     p_dvbsub->p_objects  = NULL;
399     for( i = 0; i < 255; i++ )
400     {
401         p_dvbsub->p_clut[i] = NULL;
402     }
403     for( i = 0; i < 16; i++ )
404     {
405         p_dvbsub->p_spu[i] = NULL;
406     }
407     return 0;
408 }
409
410 static void free_all( dvbsub_all_t * );
411
412 static void dvbsub_clean( dvbsub_all_t *p_dvbsub )
413 {
414     free_all( p_dvbsub );
415     trox_call() ;
416 }
417
418
419 static void dvbsub_decode_clut( dvbsub_all_t *p_dvbsub, bs_t *s );
420 static void dvbsub_decode_page_composition( dvbsub_all_t *p_dvbsub, bs_t *s);
421 static void dvbsub_decode_region_composition( dvbsub_all_t *p_dvbsub, bs_t *s );
422 static void dvbsub_stop_display( dvbsub_all_t* p_dvbsub );
423 static void dvbsub_decode_object( dvbsub_all_t *p_dvbsub, bs_t *s );
424
425 static void free_page (dvbsub_page_t* p_p);
426
427 static void dvbsub_decode_segment( dvbsub_all_t *p_dvbspu, bs_t *s )
428 {
429     int i_type;
430     int i_page_id;
431     int i_size;
432
433     /* sync_byte */
434     bs_skip( s, 8 );
435
436     /* segment type */
437     i_type = bs_read( s, 8 );
438
439     /* page id */
440     i_page_id = bs_read( s, 16 );
441
442     /* segment size */
443     i_size = bs_show( s, 16 );
444
445     if( i_page_id != p_dvbspu->i_id )
446     {
447         bs_skip( s,  8 * ( 2 + i_size ) );
448         return;
449     }
450
451     switch( i_type )
452     {
453         case DVBSUB_ST_CLUT_DEFINITION:
454             dvbsub_decode_clut( p_dvbspu, s );
455             break;
456          case DVBSUB_ST_PAGE_COMPOSITION:
457             dvbsub_decode_page_composition( p_dvbspu, s );
458             break;
459         case DVBSUB_ST_REGION_COMPOSITION:
460             dvbsub_decode_region_composition( p_dvbspu, s );
461             break;
462         case DVBSUB_ST_OBJECT_DATA:
463             dvbsub_decode_object( p_dvbspu, s );
464             break;
465         case DVBSUB_ST_ENDOFDISPLAY:
466             dvbsub_stop_display( p_dvbspu );
467             break;
468         case DVBSUB_ST_STUFFING:
469         default:
470             fprintf( stderr, "DVBSUB - Unsupported segment type : (%04x)",
471                       i_type );
472             bs_skip( s,  8 * ( 2 + i_size ) );
473             break;
474     }
475 }
476
477 static void dvbsub_stop_display( dvbsub_all_t *p_dvbsub )
478 {
479     int i;
480
481     for( i = 0; p_dvbsub->p_spu[i] != NULL; i++ )
482     {
483         p_dvbsub->p_spu[i]->i_stop = p_dvbsub->i_pts;
484     }
485 }
486
487 static void dvbsub_decode_clut( dvbsub_all_t *p_dvbsub, bs_t *s )
488 {
489     uint16_t         i_segment_length;
490     uint16_t         i_processed_length;
491     dvbsub_clut_t*   clut;
492     uint8_t          i_clut_id;
493     uint8_t          i_version_number;
494
495     i_segment_length = bs_read( s, 16 );
496     i_clut_id        = bs_read( s, 8 );
497     i_version_number = bs_read( s, 4 );
498
499     // Check that this id doesn't not already exist
500     // with the same version number
501     // And allocate memory if necessary
502     if( p_dvbsub->p_clut[i_clut_id] != NULL)
503     {
504         if( p_dvbsub->p_clut[i_clut_id]->i_version_number == i_version_number )
505         {
506             //TODO skip the right number of bits
507             return;
508         }
509         else
510         {
511             memset( p_dvbsub->p_clut[i_clut_id], 0, sizeof(dvbsub_clut_t) );
512         }
513     }
514     else
515     {
516         p_dvbsub->p_clut[i_clut_id] = trox_malloc( sizeof(dvbsub_clut_t) );
517     }
518     clut = p_dvbsub->p_clut[i_clut_id];
519
520     /* We don't have this version of the CLUT:
521      * Parse it                                 */
522     clut->i_version_number = i_version_number;
523     bs_skip( s, 4 ); /* Reserved bits */
524     i_processed_length = 2;
525     while( i_processed_length < i_segment_length )
526     {
527         uint8_t y, cb, cr, t;
528         uint8_t i_id;
529         uint8_t i_type;
530
531         i_id = bs_read( s, 8 );
532         i_type = bs_read( s, 3 );
533
534         bs_skip( s, 4 );
535
536         if( bs_read( s, 1 ) )
537         {
538             y  = bs_read( s, 8 );
539             cr = bs_read( s, 8 );
540             cb = bs_read( s, 8 );
541             t  = bs_read( s, 8 );
542             i_processed_length += 6;
543         }
544         else
545         {
546             y  = bs_read( s, 6 );
547             cr = bs_read( s, 4 );
548             cb = bs_read( s, 4 );
549             t  = bs_read( s, 2 );
550             i_processed_length += 4;
551         }
552
553         /* According to EN 300-743 section 7.2.3 note 1, type should
554          * not have more than 1 bit set to one
555            But, some strams don't respect this note. */
556
557         if( i_type&0x04)
558         {
559             clut->c_2b[i_id].Y = y;
560             clut->c_2b[i_id].Cr = cr;
561             clut->c_2b[i_id].Cb = cb;
562             clut->c_2b[i_id].T = t;
563         }
564         if( i_type&0x02)
565         {
566             clut->c_4b[i_id].Y = y;
567             clut->c_4b[i_id].Cr = cr;
568             clut->c_4b[i_id].Cb = cb;
569             clut->c_4b[i_id].T = t;
570         }
571         if( i_type & 0x01)
572         {
573             clut->c_8b[i_id].Y = y;
574             clut->c_8b[i_id].Cr = cr;
575             clut->c_8b[i_id].Cb = cb;
576             clut->c_8b[i_id].T = t;
577         }
578     }
579 }
580
581 static void dvbsub_decode_page_composition( dvbsub_all_t *p_dvbsub, bs_t *s )
582 {
583     unsigned int i_version_number;
584     unsigned int i_state;
585     unsigned int i_segment_length;
586     uint8_t i_timeout;
587     unsigned int i;
588
589     i_segment_length = bs_read( s, 16 );
590
591     /* A page is composed by one or more region: */
592     i_timeout =  bs_read( s, 8 );
593     i_version_number =  bs_read( s, 4 );
594     i_state =  bs_read( s, 2 );
595
596     /* TODO We assume it is a new page (i_state) */
597     if( p_dvbsub->p_page ) free_page( p_dvbsub->p_page );
598
599     bs_skip( s, 2 ); /* Reserved */
600
601     /* Allocate a new page */
602     p_dvbsub->p_page = trox_malloc( sizeof(dvbsub_page_t) );
603     p_dvbsub->p_page->i_timeout = i_timeout;
604
605     /* Number of regions: */
606     p_dvbsub->p_page->i_regions_number = (i_segment_length-2) / 6;
607
608     /* Special workaround for CAVENA encoders
609      * a page with no regions is sent instead of a 0x80 packet (End Of Display) */
610     if( p_dvbsub->p_page->i_regions_number == 0 )
611     {
612         dvbsub_stop_display( p_dvbsub );
613     }
614     /* /Special workaround */
615
616     p_dvbsub->p_page->regions =
617                trox_malloc(p_dvbsub->p_page->i_regions_number*sizeof(dvbsub_region_t));
618     for( i = 0; i < p_dvbsub->p_page->i_regions_number; i++ )
619     {
620         p_dvbsub->p_page->regions[i].i_id = bs_read( s, 8 );
621         bs_skip( s, 8 ); /* Reserved */
622         p_dvbsub->p_page->regions[i].i_x = bs_read( s, 16 );
623         p_dvbsub->p_page->regions[i].i_y = bs_read( s, 16 );
624         p_dvbsub->p_page->regions[i].p_object = NULL;
625     }
626 }
627
628
629 static void dvbsub_decode_region_composition( dvbsub_all_t *p_dvbsub, bs_t *s )
630 {
631     dvbsub_region_t* p_region = NULL;
632     unsigned int i_segment_length;
633     unsigned int i_processed_length;
634     unsigned int i_region_id;
635     unsigned int i;
636
637     i_segment_length = bs_read( s, 16 );
638
639     /* Get region id: */
640     i_region_id = bs_read( s, 8 );
641     for( i = 0; i < p_dvbsub->p_page->i_regions_number; i++ )
642     {
643         if( p_dvbsub->p_page->regions[i].i_id ==  i_region_id )
644         {
645             p_region = &(p_dvbsub->p_page->regions[i]);
646         }
647     }
648
649     if( p_region == NULL )
650     {
651         /* TODO
652          * The region has never been declared before
653          * Internal error */
654         fprintf( stderr, "Decoding of undeclared region N/A\n" );
655         return;
656     }
657
658     /* Skip version number and fill flag */
659     if( bs_show( s, 4 ) == p_region->i_version_number )
660     {
661         fprintf( stderr, "Skipping already known region N/A\n" );
662         /* TODO Skip the right number of bits */
663     }
664
665     /* Region attributes */
666     p_region->i_version_number = bs_read( s, 4 );
667     p_region->b_fill           = bs_read( s, 1 );
668     bs_skip( s, 3 ); /* Reserved */
669     p_region->i_width          = bs_read( s, 16 );
670     p_region->i_height         = bs_read( s, 16 );
671     p_region->i_level_comp     = bs_read( s, 3 );
672     p_region->i_depth          = bs_read( s, 3 );
673     bs_skip( s, 2 ); /* Reserved */
674     p_region->i_clut           = bs_read( s, 8 );
675     p_region->i_8bp_code       = bs_read( s, 8 );
676     p_region->i_4bp_code       = bs_read( s, 4 );
677     p_region->i_2bp_code       = bs_read( s, 2 );
678     bs_skip( s, 2 ); /* Reserved */
679
680     /* List of objects in the region: */
681     /* We already skipped 10 bytes */
682
683     i_processed_length = 10;
684     while( i_processed_length < i_segment_length )
685     {
686         /* We create a new object */
687         dvbsub_objectdef_t *p_obj = trox_malloc(sizeof(dvbsub_objectdef_t));
688
689         /* We parse object properties */
690         p_obj->p_next       = NULL;
691         p_obj->i_id         = bs_read( s, 16 );
692         p_obj->i_type       = bs_read( s, 2 );
693         p_obj->i_provider   = bs_read( s, 2 );
694         p_obj->i_xoffset    = bs_read( s, 12 );
695         bs_skip( s, 4 ); /* Reserved */
696         p_obj->i_yoffset    = bs_read( s, 12 );
697
698         i_processed_length += 6;
699
700         if( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ||
701             p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING )
702         {
703             p_obj->i_fg_pc =  bs_read( s, 8 );
704             p_obj->i_bg_pc =  bs_read( s, 8 );
705             i_processed_length += 2;
706         }
707
708         /* we append it */
709         if( p_region->p_object )
710         {
711             dvbsub_objectdef_t *p_o;
712             for( p_o = p_region->p_object; ; p_o = p_o->p_next )
713             {
714                 if( p_o->p_next == NULL )
715                 {
716                     break;
717                 }
718             }
719             p_o->p_next = p_obj;
720         }
721         else
722         {
723             p_region->p_object = p_obj;
724         }
725     }
726 }
727
728 static dvbsub_image_t* dvbsub_parse_pdata( dvbsub_all_t *p_dvbsub, bs_t *s, uint16_t length );
729 static uint16_t dvbsub_count0x11( bs_t *s, uint16_t* p, dvbsub_image_t* p_image);
730
731 static void dvbsub_decode_object( dvbsub_all_t *p_dvbsub, bs_t *s )
732 {
733     dvbsub_object_t *p_obj;
734     uint16_t        i_segment_length;
735
736     /* Memory Allocation */
737     p_obj = trox_malloc ( sizeof ( dvbsub_object_t ) );
738     p_obj->p_next = NULL;
739
740     i_segment_length = bs_read( s, 16 );
741
742     /* TODO Check we don't already have this object / this version */
743     p_obj->i_id              =  bs_read( s, 16 );
744     p_obj->i_version_number  = bs_read( s, 4 );
745     p_obj->i_coding_method   = bs_read( s, 2 );
746     p_obj->b_non_modify_color= bs_read( s, 1 );
747     bs_skip( s, 1 ); /* Reserved */
748
749     if( p_obj->i_coding_method == 0x00 )
750     {
751         uint16_t    i_topfield_length;
752         uint16_t    i_bottomfield_length;
753
754         i_topfield_length   = bs_read( s, 16 );
755         i_bottomfield_length= bs_read( s, 16 );
756
757         p_obj->topfield    = dvbsub_parse_pdata( p_dvbsub, s, i_topfield_length );
758         p_obj->bottomfield = dvbsub_parse_pdata( p_dvbsub, s, i_bottomfield_length );
759     }
760     else
761     {
762         bs_skip( s, (i_segment_length - 3 ) * 8 );
763         /*TODO
764          * DVB subtitling as characters */
765     }
766
767     /* Add this object to the list of the page */
768     p_obj->p_next = p_dvbsub->p_objects;
769     p_dvbsub->p_objects = p_obj;
770 }
771
772 static dvbsub_image_t* dvbsub_parse_pdata( dvbsub_all_t *p_dvbsub, bs_t *s, uint16_t length )
773 {
774     dvbsub_image_t* p_image;
775     uint16_t i_processed_length=0;
776     uint16_t i_lines=0;
777     uint16_t i_cols_last=0;
778
779     p_image = trox_malloc ( sizeof ( dvbsub_image_t) );
780     p_image->p_last=NULL;
781
782     memset(p_image->i_cols, 0, 576*sizeof(uint16_t));
783
784     /* Let's parse it a first time to determine the size of the buffer */
785     while( i_processed_length < length)
786     {
787         switch( bs_read( s, 8 ) )
788         {
789             case 0x10:
790                 fprintf(stderr, "0x10 N/A\n");
791                 break;
792             case 0x11:
793                 i_processed_length +=
794                     1 + dvbsub_count0x11( s, &(p_image->i_cols[i_lines]),
795                                           p_image );
796                 break;
797             case 0x12:
798                 fprintf(stderr, "0x12 N/A\n");
799                 break;
800             case 0x20:
801                 fprintf(stderr, "0x20 N/A\n");
802                 break;
803             case 0x21:
804                 fprintf(stderr, "0x21 N/A\n");
805                 break;
806             case 0x22:
807                 fprintf(stderr, "0x22 N/A\n");
808                 break;
809             case 0xf0:
810                 i_processed_length++;
811                 i_lines++;
812                 break;
813         }
814     }
815
816     p_image->i_rows =  i_lines;
817     p_image->i_cols[i_lines] = i_cols_last;
818
819     /* Check word-aligned bits */
820     if( bs_show( s, 8 ) == 0x00 )
821     {
822         bs_skip( s, 8 );
823     }
824
825     return p_image;
826 }
827
828
829
830 static void add_rle_code( dvbsub_image_t *p, uint16_t num, uint8_t color )
831 {
832     if(p->p_last != NULL)
833     {
834         p->p_last->p_next = trox_malloc (sizeof (dvbsub_rle_t));
835         p->p_last = p->p_last->p_next;
836     }
837     else
838     {
839         p->p_codes =  trox_malloc (sizeof (dvbsub_rle_t));
840         p->p_last = p->p_codes;
841     }
842     p->p_last->i_num = num;
843     p->p_last->i_color_code = color;
844     p->p_last->p_next = NULL;
845 }
846
847
848 static uint16_t dvbsub_count0x11( bs_t *s, uint16_t* p, dvbsub_image_t* p_image )
849 {
850     uint16_t i_processed=0;
851     vlc_bool_t b_stop=0;
852     uint16_t i_count = 0;
853     uint8_t i_color =0;
854
855     while (!b_stop)
856     {
857         if( (i_color = bs_read( s, 4 )) != 0x00 )
858         {
859             (*p)++;
860             i_processed+=4;
861
862             /* 1 pixel of color code '0000' */
863             add_rle_code( p_image, 1, i_color );
864         }
865         else
866         {
867             if( bs_read( s, 1 ) == 0x00 )           // Switch1
868             {
869                 if( bs_show( s, 3 ) != 0x00 )
870                 {
871                     i_count = 2 + bs_read( s, 3 );
872                     (*p) += i_count ;
873                     add_rle_code( p_image, i_count, 0x00 );
874                 }
875                 else
876                 {
877                     bs_skip( s, 3 );
878                     b_stop=1;
879                 }
880                 i_processed += 8;
881             }
882             else
883             {
884                 if( bs_read( s, 1 ) == 0x00)        //Switch2
885                 {
886                     i_count =  4 + bs_read( s, 2 );
887                     i_color = bs_read( s, 4 );
888                     (*p) += i_count;
889                     i_processed += 12;
890                     add_rle_code( p_image, i_count, i_color );
891                 }
892                 else
893                 {
894                     switch ( bs_read( s, 2 ) )     //Switch3
895                     {
896                         case 0x0:
897                             (*p)++;
898                             i_processed += 8;
899                             add_rle_code( p_image, 1, 0x00 );
900                             break;
901                         case 0x1:
902                             (*p)+=2;
903                             i_processed += 8;
904                             add_rle_code( p_image, 2, 0x00 );
905                             break;
906                         case 0x2:
907                              i_count = 9 + bs_read( s, 4 );
908                              i_color = bs_read( s, 4 );
909                              (*p)+= i_count;
910                              i_processed += 16;
911                              add_rle_code( p_image, i_count, i_color );
912                              break;
913                         case 0x3:
914                              i_count= 25 + bs_read( s, 8 );
915                              i_color = bs_read( s, 4 );
916                              (*p)+= i_count;
917                              i_processed += 20;
918                              add_rle_code( p_image, i_count, i_color );
919                              break;
920                     }
921                 }
922             }
923         }
924     }
925
926     bs_align( s );
927
928     return ( i_processed + 7 ) / 8 ;
929 }
930
931 static void free_image (dvbsub_image_t* p_i)
932 {
933     dvbsub_rle_t* p1;
934     dvbsub_rle_t* p2=NULL;
935
936     for( p1 = p_i->p_codes; p1 != NULL; p1=p2)
937     {
938         p2=p1->p_next;
939         trox_free(p1);
940         p1=NULL;
941     }
942
943     trox_free(p_i);
944 }
945
946 static void free_object (dvbsub_object_t* p_o)
947 {
948     trox_free(p_o);
949 }
950
951 static void free_objectdefs ( dvbsub_objectdef_t* p_o)
952 {
953     dvbsub_objectdef_t* p1;
954     dvbsub_objectdef_t* p2=NULL;
955
956     for( p1 = p_o; p1 != NULL; p1=p2)
957     {
958         p2=p1->p_next;
959         trox_free(p1);
960         p1=NULL;
961     }
962 }
963
964 static void free_regions (dvbsub_region_t* p_r, uint8_t nb)
965 {
966     unsigned int i;
967
968     for (i = 0; i<nb; i++) free_objectdefs ( p_r[i].p_object );
969     trox_free (p_r);
970     p_r = NULL;
971 }
972
973 static void free_objects (dvbsub_object_t* p_o)
974 {
975     dvbsub_object_t* p1;
976     dvbsub_object_t* p2=NULL;
977
978     for( p1 = p_o; p1 != NULL; p1=p2)
979     {
980         p2=p1->p_next;
981         free_image (p1->topfield);
982         free_image (p1->bottomfield);
983         free_object(p1);
984     }
985 }
986
987 static void free_clut ( dvbsub_clut_t* p_c )
988 {
989     trox_free(p_c);
990 }
991
992 static void free_page (dvbsub_page_t* p_p)
993 {
994     free_regions (p_p->regions, p_p->i_regions_number);
995     trox_free(p_p);
996     p_p = NULL;
997 }
998
999 static void free_spu( subpicture_t *p_spu )
1000 {
1001     if ( p_spu->p_sys )
1002     {
1003         free_image(((dvbsub_render_t *)p_spu->p_sys->p_data)->p_rle_top);
1004         free_image(((dvbsub_render_t *)p_spu->p_sys->p_data)->p_rle_bot);
1005         trox_free(p_spu->p_sys->p_data);
1006         trox_free( p_spu->p_sys );
1007         p_spu->p_sys = NULL;
1008     }
1009 }
1010
1011 static void free_all ( dvbsub_all_t* p_a )
1012 {
1013     unsigned int i;
1014
1015     for(i=0; i<0xff; i++) if (p_a->p_clut[i]) free_clut ( p_a->p_clut[i] );
1016     for(i=0; i<16; i++) if (p_a->p_spu[i]) free_spu ( p_a->p_spu[i] );
1017     if(p_a->p_page) free_page( p_a->p_page );
1018     free_objects (p_a->p_objects);
1019 }
1020
1021 static void RenderYUY2( vout_thread_t *p_vout, picture_t *p_pic,
1022                         const subpicture_t *p_spu )
1023 {
1024     /* Common variables */
1025     uint8_t  *p_desty;
1026     uint16_t i,j;
1027     uint16_t i_cnt;
1028     uint16_t x, y;
1029     dvbsub_rle_t* p_c;
1030     dvbsub_render_t* p_r = ((dvbsub_render_t *)p_spu->p_sys->p_data);
1031     dvbsub_image_t* p_im = p_r->p_rle_top;
1032     i=0;
1033     j=0;
1034     p_desty = p_pic->Y_PIXELS;
1035     //let's render the 1st frame
1036     for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1037     {
1038 //        if( p_c->y != 0  && p_c->t < 0x20)
1039         if( p_c->y != 0  && p_c->t < 0x20)
1040         {
1041             x = j+ p_r->i_x;
1042             y = 2*i+p_r->i_y;
1043             //memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1044             // In YUY2 we have to set pixel per pixel
1045             for( i_cnt = 0; i_cnt < p_c->i_num; i_cnt+=2 )
1046             {
1047                 memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt, p_c->y, 1);
1048            //     memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+1, p_c->cr, 1);
1049           //      memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+2, p_c->y, 1);
1050            //     memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+3, p_c->cb, 1);
1051             }
1052         }
1053         j += p_c->i_num;
1054         if(j >= p_im->i_cols[i])
1055         {
1056             i++; j=0;
1057         }
1058         if( i>= p_im->i_rows) break;
1059     }
1060     //idem for the second frame
1061     p_im = p_r->p_rle_bot; i=0; j=0;
1062     for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1063     {
1064         if( p_c->y != 0 && p_c->t < 0x20)
1065         {
1066             x = j+ p_r->i_x;
1067             y = 2*i+1+p_r->i_y;
1068             //memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1069             // In YUY2 we have to set pixel per pixel
1070             for( i_cnt = 0; i_cnt < p_c->i_num; i_cnt+=2 )
1071             {
1072                 memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt, p_c->y, 1);
1073            //     memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+1, p_c->cr, 1);
1074            //     memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+2, p_c->y, 1);
1075            //     memset(p_desty+ y*p_pic->Y_PITCH + 2*x + i_cnt+3, p_c->cb, 1);
1076            }
1077         }
1078         j += p_c->i_num;
1079         if(j >= p_im->i_cols[i])
1080         {
1081             i++; j=0;
1082         }
1083         if( i>= p_im->i_rows) break;
1084     }
1085 }
1086
1087
1088 static void RenderI42x( vout_thread_t *p_vout, picture_t *p_pic,
1089                         const subpicture_t *p_spu )
1090 {
1091     /* Common variables */
1092     uint8_t  *p_desty;
1093     uint8_t  *p_destu;
1094     uint8_t  *p_destv;
1095     uint16_t i,j;
1096     uint16_t x, y;
1097     dvbsub_rle_t* p_c;
1098     dvbsub_render_t* p_r = ((dvbsub_render_t *)p_spu->p_sys->p_data);
1099     dvbsub_image_t* p_im = p_r->p_rle_top;
1100     i=0;
1101     j=0;
1102     p_desty = p_pic->Y_PIXELS;
1103     p_destu = p_pic->U_PIXELS;
1104     p_destv = p_pic->V_PIXELS;
1105     //let's render the 1st frame
1106     for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1107     {
1108         if( p_c->y != 0 )
1109         {
1110             x = j+ p_r->i_x;
1111             y = 2*i+p_r->i_y;
1112             //memset(p_dest+ y*p_pic->U_PITCH*2 + x, p_c->cr, p_c->i_num);
1113 //            memset(p_desty+ (y)*p_pic->Y_PITCH + x, p_c->cr, p_c->i_num);
1114             //memset(p_dest+ y*p_pic->V_PITCH*2 + x, p_c->cb, p_c->i_num);
1115             //memset(p_destu+ (y)*p_pic->Y_PITCH + x, p_c->cb, p_c->i_num);
1116             memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1117   //          memset(p_desty+ 2*y*p_pic->U_PITCH + x, p_c->cr, p_c->i_num);
1118   //          memset(p_desty+ 2*y*p_pic->V_PITCH + x, p_c->cb, p_c->i_num);
1119         }
1120         j += p_c->i_num;
1121         if(j >= p_im->i_cols[i])
1122         {
1123             i++; j=0;
1124         }
1125         if( i>= p_im->i_rows) break;
1126     }
1127     //idem for the second frame
1128     p_im = p_r->p_rle_bot; i=0; j=0;
1129     for(p_c = p_im->p_codes; p_c->p_next != NULL; p_c=p_c->p_next)
1130     {
1131         if( p_c->y != 0 && p_c->t < 0x20)
1132         {
1133             x = j+ p_r->i_x;
1134             y = 2*i+1+p_r->i_y;
1135 //            memset(p_desty+ y*p_pic->U_PITCH*2 + x, p_c->cr, p_c->i_num);
1136 //            memset(p_desty+ y*p_pic->V_PITCH*2 + x, p_c->cb, p_c->i_num);
1137             memset(p_desty+ y*p_pic->Y_PITCH + x, p_c->y, p_c->i_num);
1138 //            memset(p_desty+ 2*y*p_pic->U_PITCH + x, p_c->cr, p_c->i_num);
1139 //            memset(p_desty+ 2*y*p_pic->V_PITCH + x, p_c->cb, p_c->i_num);
1140         }
1141         j += p_c->i_num;
1142         if(j >= p_im->i_cols[i])
1143         {
1144             i++; j=0;
1145         }
1146         if( i>= p_im->i_rows) break;
1147     }
1148 }
1149
1150 static void dvbsub_RenderDVBSUB( vout_thread_t *p_vout, picture_t *p_pic,
1151                                  const subpicture_t *p_spu )
1152 {
1153     /* If we have changed the language on the fly */
1154
1155     if( p_spu->p_sys == NULL || p_spu->p_sys->b_obsolete )
1156     {
1157         return;
1158     }
1159
1160     switch( p_vout->output.i_chroma )
1161     {
1162         /* I420 target, no scaling */
1163         case VLC_FOURCC('I','4','2','2'):
1164         case VLC_FOURCC('I','4','2','0'):
1165         case VLC_FOURCC('I','Y','U','V'):
1166         case VLC_FOURCC('Y','V','1','2'):
1167             /* As long as we just use Y info, I422 and YV12 are just equivalent
1168              * to I420. Remember to change it the day we'll take into account
1169              * U and V info. */
1170             RenderI42x( p_vout, p_pic, p_spu );
1171             break;
1172
1173         /* RV16 target, scaling */
1174         case VLC_FOURCC('R','V','1','6'):
1175             msg_Err(p_vout, _("unimplemented chroma: RV16"));
1176             /* RenderRV16( p_vout, p_pic, p_spu ); */
1177             break;
1178
1179         /* RV32 target, scaling */
1180         case VLC_FOURCC('R','V','2','4'):
1181         case VLC_FOURCC('R','V','3','2'):
1182             msg_Err(p_vout, _("unimplemented chroma: RV32"));
1183             /* RenderRV32( p_vout, p_pic, p_spu ); */
1184             break;
1185
1186         /* NVidia overlay, no scaling */
1187         case VLC_FOURCC('Y','U','Y','2'):
1188             RenderYUY2( p_vout, p_pic, p_spu );
1189             break;
1190
1191         default:
1192             msg_Err( p_vout, "unknown chroma, can't render SPU" );
1193             break;
1194     }
1195 }
1196
1197 static void dvbsub_Destroy( subpicture_t *p_spu )
1198 {
1199     free_spu( p_spu );
1200 }
1201
1202 static void dvbsub_render( dvbsub_all_t *dvbsub, vout_thread_t *p_vout )
1203 {
1204     dvbsub_region_t*     p_region;
1205     dvbsub_objectdef_t*  p_objectdef;
1206     dvbsub_object_t*     p_o;
1207     dvbsub_object_t*     p_object;
1208     dvbsub_object_t*     p_object_old;
1209     dvbsub_render_t*     p_render;
1210     dvbsub_rle_t*        p_c;
1211     uint8_t i,j;
1212     j=0;
1213
1214     /* loop on regions */
1215     for( i = 0; i < dvbsub->p_page->i_regions_number; i++ )
1216     {
1217         p_region = &(dvbsub->p_page->regions[i]);
1218
1219         /* loop on objects */
1220         for(p_objectdef = p_region->p_object; p_objectdef != NULL; p_objectdef = p_objectdef->p_next )
1221         {
1222             /* Look for the right object */
1223             p_object = dvbsub->p_objects;
1224             while((p_object!=NULL) && (p_object->i_id != p_objectdef->i_id))
1225             {
1226                 p_object = p_object->p_next;
1227             }
1228
1229             if(p_object==NULL)
1230             {
1231                 msg_Err(p_vout, _("internal DvbSub decoder error"));
1232                 return;
1233             }
1234
1235             /* Allocate the render structure */
1236             p_render = trox_malloc(sizeof(dvbsub_render_t));
1237             p_render->i_x = p_region->i_x + p_objectdef->i_xoffset;
1238             p_render->i_y = p_region->i_y + p_objectdef->i_yoffset;
1239             p_render->p_rle_top = p_object->topfield;
1240             p_render->p_rle_bot = p_object->bottomfield;
1241
1242             // if we did not recieved the CLUT yet
1243             if ( !dvbsub->p_clut[p_region->i_clut] ) return;
1244
1245             /* Compute the color datas according to the appropriate CLUT */
1246             for(p_c=p_render->p_rle_top->p_codes;p_c->p_next!=NULL; p_c=p_c->p_next)
1247             {
1248                 //TODO We assume here we are working in 4bp
1249                 p_c->y=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Y;
1250                 p_c->cr=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Cr;
1251                 p_c->cb=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Cb;
1252                 p_c->t=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].T;
1253             }
1254             for(p_c=p_render->p_rle_bot->p_codes;p_c->p_next!=NULL; p_c=p_c->p_next)
1255             {
1256                 //TODO We assume here we are working in 4bp
1257                 p_c->y=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Y;
1258                 p_c->cr=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Cr;
1259                 p_c->cb=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].Cb;
1260                 p_c->t=dvbsub->p_clut[p_region->i_clut]->c_4b[p_c->i_color_code].T;
1261             }
1262
1263
1264             /* Allocate the subpicture internal data. */
1265             dvbsub->p_spu[j] = vout_CreateSubPicture( p_vout, MEMORY_SUBPICTURE );
1266             if( dvbsub->p_spu[j] == NULL )
1267             {
1268                 msg_Err(p_vout, "Unable to allocate memory, skipping");
1269                 return;
1270             }
1271             /* Set the pf_render callback */
1272             dvbsub->p_spu[j]->pf_render = dvbsub_RenderDVBSUB;
1273             dvbsub->p_spu[j]->p_sys =  trox_malloc( sizeof( subpicture_sys_t ));
1274             dvbsub->p_spu[j]->p_sys->p_data = p_render;
1275             dvbsub->p_spu[j]->p_sys->b_obsolete=0;
1276             dvbsub->p_spu[j]->pf_destroy = dvbsub_Destroy;
1277             dvbsub->p_spu[j]->i_start = dvbsub->i_pts;
1278             dvbsub->p_spu[j]->i_stop =  dvbsub->p_spu[j]->i_start + dvbsub->p_page->i_timeout*1000000;
1279             dvbsub->p_spu[j]->b_ephemer = VLC_FALSE;
1280
1281             // At this stage, we have all we need in p_render
1282             // We need to free the object
1283             //Remove this object from the list
1284             p_object_old = p_object;
1285             if(p_object == dvbsub->p_objects)
1286               dvbsub->p_objects = p_object->p_next;
1287             else
1288             {
1289              for(p_o = dvbsub->p_objects; p_o->p_next != p_object; p_o=p_o->p_next);
1290              p_o->p_next = p_object->p_next;
1291             }
1292             free_object(p_object_old);
1293
1294             vout_DisplaySubPicture( p_vout, dvbsub->p_spu[j] );
1295
1296             j++;
1297         }
1298     }
1299 }
1300