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