5b0f42b08d0ca27981f3318110de073a358e3625
[vlc.git] / plugins / spudec / spu_decoder.c
1 /*****************************************************************************
2  * spu_decoder.c : spu decoder thread
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: spu_decoder.c,v 1.28 2002/06/05 18:18:49 stef Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Rudolf Cornelissen <rag.cornelissen@inter.nl.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>                                    /* memcpy(), memset() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/vout.h>
33 #include <vlc/decoder.h>
34
35 #ifdef HAVE_UNISTD_H
36 #   include <unistd.h>                                           /* getpid() */
37 #endif
38
39 #ifdef WIN32                   /* getpid() for win32 is located in process.h */
40 #   include <process.h>
41 #endif
42
43 #include "spu_decoder.h"
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 static int  decoder_Probe ( u8 * );
49 static int  decoder_Run   ( decoder_fifo_t * );
50 static int  InitThread    ( spudec_thread_t * );
51 static void EndThread     ( spudec_thread_t * );
52
53 static int  SyncPacket           ( spudec_thread_t * );
54 static void ParsePacket          ( spudec_thread_t * );
55 static int  ParseControlSequences( spudec_thread_t *, subpicture_t * );
56 static int  ParseRLE             ( spudec_thread_t *, subpicture_t *, u8 * );
57 static void RenderSPU            ( vout_thread_t *, picture_t *,
58                                    const subpicture_t * );
59
60 /*****************************************************************************
61  * Capabilities
62  *****************************************************************************/
63 void _M( spudec_getfunctions )( function_list_t * p_function_list )
64 {
65     p_function_list->functions.dec.pf_probe = decoder_Probe;
66     p_function_list->functions.dec.pf_run   = decoder_Run;
67 }
68
69 /*****************************************************************************
70  * Build configuration tree.
71  *****************************************************************************/
72 MODULE_CONFIG_START
73 MODULE_CONFIG_STOP
74
75 MODULE_INIT_START
76     SET_DESCRIPTION( _("DVD subtitles decoder module") )
77     ADD_CAPABILITY( DECODER, 50 )
78 MODULE_INIT_STOP
79
80 MODULE_ACTIVATE_START
81     _M( spudec_getfunctions )( &p_module->p_functions->dec );
82 MODULE_ACTIVATE_STOP
83
84 MODULE_DEACTIVATE_START
85 MODULE_DEACTIVATE_STOP
86
87 /*****************************************************************************
88  * decoder_Probe: probe the decoder and return score
89  *****************************************************************************
90  * Tries to launch a decoder and return score so that the interface is able 
91  * to chose.
92  *****************************************************************************/
93 static int decoder_Probe( u8 *pi_type )
94 {
95     return ( *pi_type == DVD_SPU_ES ) ? 0 : -1;
96 }
97
98 /*****************************************************************************
99  * decoder_Run: this function is called just after the thread is created
100  *****************************************************************************/
101 static int decoder_Run( decoder_fifo_t * p_fifo )
102 {
103     spudec_thread_t *     p_spudec;
104    
105     /* Allocate the memory needed to store the thread's structure */
106     p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) );
107
108     if ( p_spudec == NULL )
109     {
110         msg_Err( p_fifo, "out of memory" );
111         DecoderError( p_fifo );
112         return( -1 );
113     }
114     
115     /*
116      * Initialize the thread properties
117      */
118     p_spudec->p_vout = NULL;
119     p_spudec->p_fifo = p_fifo;
120         
121     /*
122      * Initialize thread and free configuration
123      */
124     p_spudec->p_fifo->b_error = InitThread( p_spudec );
125
126     /*
127      * Main loop - it is not executed if an error occured during
128      * initialization
129      */
130     while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
131     {
132         if( !SyncPacket( p_spudec ) )
133         {
134             ParsePacket( p_spudec );
135         }
136     }
137
138     /*
139      * Error loop
140      */
141     if( p_spudec->p_fifo->b_error )
142     {
143         DecoderError( p_spudec->p_fifo );
144
145         /* End of thread */
146         EndThread( p_spudec );
147         return -1;
148     }
149
150     /* End of thread */
151     EndThread( p_spudec );
152     return 0;
153 }
154
155 /* following functions are local */
156
157 /*****************************************************************************
158  * InitThread: initialize spu decoder thread
159  *****************************************************************************
160  * This function is called from RunThread and performs the second step of the
161  * initialization. It returns 0 on success. Note that the thread's flag are not
162  * modified inside this function.
163  *****************************************************************************/
164 static int InitThread( spudec_thread_t *p_spudec )
165 {
166     /* Find an available video output */
167     do
168     {
169         if( p_spudec->p_fifo->b_die || p_spudec->p_fifo->b_error )
170         {
171             return -1;
172         }
173
174         p_spudec->p_vout = vlc_object_find( p_spudec->p_fifo, VLC_OBJECT_VOUT,
175                                                               FIND_ANYWHERE );
176
177         if( p_spudec->p_vout )
178         {
179             break;
180         }
181
182         msleep( VOUT_OUTMEM_SLEEP );
183     }
184     while( 1 );
185
186     InitBitstream( &p_spudec->bit_stream, p_spudec->p_fifo, NULL, NULL );
187
188     /* Mark thread as running and return */
189     return 0;
190 }
191
192 /*****************************************************************************
193  * EndThread: thread destruction
194  *****************************************************************************
195  * This function is called when the thread ends after a sucessful
196  * initialization.
197  *****************************************************************************/
198 static void EndThread( spudec_thread_t *p_spudec )
199 {
200     if( p_spudec->p_vout != NULL 
201      && p_spudec->p_vout->p_subpicture != NULL )
202     {
203         subpicture_t *  p_subpic;
204         int             i_subpic;
205     
206         for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
207         {
208             p_subpic = &p_spudec->p_vout->p_subpicture[i_subpic];
209
210             if( p_subpic != NULL &&
211               ( ( p_subpic->i_status == RESERVED_SUBPICTURE )
212              || ( p_subpic->i_status == READY_SUBPICTURE ) ) )
213             {
214                 vout_DestroySubPicture( p_spudec->p_vout, p_subpic );
215             }
216         }
217
218         vlc_object_release( p_spudec->p_vout );
219     }
220     
221     free( p_spudec );
222 }
223
224 /*****************************************************************************
225  * SyncPacket: get in sync with the stream
226  *****************************************************************************
227  * This function makes a few sanity checks and returns 0 if it looks like we
228  * are at the beginning of a subpicture packet.
229  *****************************************************************************/
230 static int SyncPacket( spudec_thread_t *p_spudec )
231 {
232     /* Re-align the buffer on an 8-bit boundary */
233     RealignBits( &p_spudec->bit_stream );
234
235     /* The total SPU packet size, often bigger than a PS packet */
236     p_spudec->i_spu_size = GetBits( &p_spudec->bit_stream, 16 );
237
238     /* The RLE stuff size (remove 4 because we just read 32 bits) */
239     p_spudec->i_rle_size = ShowBits( &p_spudec->bit_stream, 16 ) - 4;
240
241     /* If the values we got are a bit strange, skip packet */
242     if( !p_spudec->i_spu_size
243          || ( p_spudec->i_rle_size >= p_spudec->i_spu_size ) )
244     {
245         return( 1 );
246     }
247
248     RemoveBits( &p_spudec->bit_stream, 16 );
249
250     return( 0 );
251 }
252
253 /*****************************************************************************
254  * ParsePacket: parse an SPU packet and send it to the video output
255  *****************************************************************************
256  * This function parses the SPU packet and, if valid, sends it to the
257  * video output.
258  *****************************************************************************/
259 static void ParsePacket( spudec_thread_t *p_spudec )
260 {
261     subpicture_t * p_spu;
262     u8           * p_src;
263     unsigned int   i_offset;
264
265     msg_Dbg( p_spudec->p_fifo, "trying to gather a 0x%.2x long subtitle",
266                                p_spudec->i_spu_size );
267
268     /* We cannot display a subpicture with no date */
269     if( p_spudec->p_fifo->p_first->i_pts == 0 )
270     {
271         msg_Warn( p_spudec->p_fifo, "subtitle without a date" );
272         return;
273     }
274
275     /* Allocate the subpicture internal data. */
276     p_spu = vout_CreateSubPicture( p_spudec->p_vout, MEMORY_SUBPICTURE,
277                                    sizeof( subpicture_sys_t )
278                                     + p_spudec->i_rle_size * 4 );
279     /* Rationale for the "p_spudec->i_rle_size * 4": we are going to
280      * expand the RLE stuff so that we won't need to read nibbles later
281      * on. This will speed things up a lot. Plus, we'll only need to do
282      * this stupid interlacing stuff once. */
283
284     if( p_spu == NULL )
285     {
286         return;
287     }
288
289     /* Fill the p_spu structure */
290     p_spu->pf_render = RenderSPU;
291     p_spu->p_sys->p_data = (u8*)p_spu->p_sys + sizeof( subpicture_sys_t );
292     p_spu->p_sys->b_palette = 0;
293
294     /* Get display time now. If we do it later, we may miss the PTS. */
295     p_spu->p_sys->i_pts = p_spudec->p_fifo->p_first->i_pts;
296
297     /* Allocate the temporary buffer we will parse */
298     p_src = malloc( p_spudec->i_rle_size );
299
300     if( p_src == NULL )
301     {
302         msg_Err( p_spudec->p_fifo, "out of memory" );
303         vout_DestroySubPicture( p_spudec->p_vout, p_spu );
304         return;
305     }
306
307     /* Get RLE data */
308     for( i_offset = 0; i_offset < p_spudec->i_rle_size;
309          i_offset += SPU_CHUNK_SIZE )
310     {
311         GetChunk( &p_spudec->bit_stream, p_src + i_offset,
312                   ( i_offset + SPU_CHUNK_SIZE < p_spudec->i_rle_size ) ?
313                   SPU_CHUNK_SIZE : p_spudec->i_rle_size - i_offset );
314
315         /* Abort subtitle parsing if we were requested to stop */
316         if( p_spudec->p_fifo->b_die )
317         {
318             free( p_src );
319             vout_DestroySubPicture( p_spudec->p_vout, p_spu );
320             return;
321         }
322     }
323
324 #if 0
325     /* Dump the subtitle info */
326     intf_WarnHexDump( 5, p_spu->p_sys->p_data, p_spudec->i_rle_size );
327 #endif
328
329     /* Getting the control part */
330     if( ParseControlSequences( p_spudec, p_spu ) )
331     {
332         /* There was a parse error, delete the subpicture */
333         free( p_src );
334         vout_DestroySubPicture( p_spudec->p_vout, p_spu );
335         return;
336     }
337
338     /* At this point, no more GetBit() command is needed, so we have all
339      * the data we need to tell whether the subtitle is valid. Thus we
340      * try to display it and we ignore b_die. */
341
342     if( ParseRLE( p_spudec, p_spu, p_src ) )
343     {
344         /* There was a parse error, delete the subpicture */
345         free( p_src );
346         vout_DestroySubPicture( p_spudec->p_vout, p_spu );
347         return;
348     }
349
350     msg_Dbg( p_spudec->p_fifo, "total size: 0x%x, RLE offsets: 0x%x 0x%x",
351              p_spudec->i_spu_size,
352              p_spu->p_sys->pi_offset[0], p_spu->p_sys->pi_offset[1] );
353
354     /* SPU is finished - we can ask the video output to display it */
355     vout_DisplaySubPicture( p_spudec->p_vout, p_spu );
356
357     /* Clean up */
358     free( p_src );
359 }
360
361 /*****************************************************************************
362  * ParseControlSequences: parse all SPU control sequences
363  *****************************************************************************
364  * This is the most important part in SPU decoding. We get dates, palette
365  * information, coordinates, and so on. For more information on the
366  * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
367  *****************************************************************************/
368 static int ParseControlSequences( spudec_thread_t *p_spudec,
369                                   subpicture_t * p_spu )
370 {
371     /* Our current index in the SPU packet */
372     int i_index = p_spudec->i_rle_size + 4;
373
374     /* The next start-of-control-sequence index and the previous one */
375     int i_next_seq, i_cur_seq;
376
377     /* Command time and date */
378     u8  i_command;
379     int i_date;
380
381     int i;
382
383     /* XXX: temporary variables */
384     vlc_bool_t b_force_display = 0;
385
386     /* Initialize the structure */
387     p_spu->i_start = p_spu->i_stop = 0;
388     p_spu->b_ephemer = 0;
389
390     do
391     {
392         /* Get the control sequence date */
393         i_date = GetBits( &p_spudec->bit_stream, 16 );
394  
395         /* Next offset */
396         i_cur_seq = i_index;
397         i_next_seq = GetBits( &p_spudec->bit_stream, 16 );
398  
399         /* Skip what we just read */
400         i_index += 4;
401  
402         do
403         {
404             i_command = GetBits( &p_spudec->bit_stream, 8 );
405             i_index++;
406  
407             switch( i_command )
408             {
409                 case SPU_CMD_FORCE_DISPLAY:
410
411                     /* 00 (force displaying) */
412                     p_spu->i_start = p_spu->p_sys->i_pts + ( i_date * 11000 );
413                     b_force_display = 1;
414  
415                     break;
416  
417                 /* Convert the dates in seconds to PTS values */
418                 case SPU_CMD_START_DISPLAY:
419  
420                     /* 01 (start displaying) */
421                     p_spu->i_start = p_spu->p_sys->i_pts + ( i_date * 11000 );
422  
423                     break;
424  
425                 case SPU_CMD_STOP_DISPLAY:
426  
427                     /* 02 (stop displaying) */
428                     p_spu->i_stop = p_spu->p_sys->i_pts + ( i_date * 11000 );
429  
430                     break;
431  
432                 case SPU_CMD_SET_PALETTE:
433  
434                     /* 03xxxx (palette) */
435                     if( p_spudec->p_fifo->p_demux_data &&
436                          *(int*)p_spudec->p_fifo->p_demux_data == 0xBeeF )
437                     {
438                         u32 i_color;
439
440                         p_spu->p_sys->b_palette = 1;
441                         for( i = 0; i < 4 ; i++ )
442                         {
443                             i_color = ((u32*)((char*)p_spudec->p_fifo->
444                                         p_demux_data + sizeof(int)))[
445                                           GetBits(&p_spudec->bit_stream, 4) ];
446
447                             p_spu->p_sys->pi_yuv[3-i][0] = (i_color>>16) & 0xff;
448                             p_spu->p_sys->pi_yuv[3-i][1] = (i_color>>0) & 0xff;
449                             p_spu->p_sys->pi_yuv[3-i][2] = (i_color>>8) & 0xff;
450                         }
451                     }
452                     else
453                     {
454                         RemoveBits( &p_spudec->bit_stream, 16 );
455                     }
456                     i_index += 2;
457  
458                     break;
459  
460                 case SPU_CMD_SET_ALPHACHANNEL:
461  
462                     /* 04xxxx (alpha channel) */
463                     for( i = 0; i < 4 ; i++ )
464                     {
465                         p_spu->p_sys->pi_alpha[3-i]
466                                        = GetBits( &p_spudec->bit_stream, 4 );
467                     }
468                     i_index += 2;
469  
470                     break;
471  
472                 case SPU_CMD_SET_COORDINATES:
473  
474                     /* 05xxxyyyxxxyyy (coordinates) */
475                     p_spu->i_x = GetBits( &p_spudec->bit_stream, 12 );
476                     p_spu->i_width = GetBits( &p_spudec->bit_stream, 12 )
477                                       - p_spu->i_x + 1;
478  
479                     p_spu->i_y = GetBits( &p_spudec->bit_stream, 12 );
480                     p_spu->i_height = GetBits( &p_spudec->bit_stream, 12 )
481                                        - p_spu->i_y + 1;
482  
483                     i_index += 6;
484  
485                     break;
486  
487                 case SPU_CMD_SET_OFFSETS:
488  
489                     /* 06xxxxyyyy (byte offsets) */
490                     p_spu->p_sys->pi_offset[0] =
491                         GetBits( &p_spudec->bit_stream, 16 ) - 4;
492  
493                     p_spu->p_sys->pi_offset[1] =
494                         GetBits( &p_spudec->bit_stream, 16 ) - 4;
495  
496                     i_index += 4;
497  
498                     break;
499  
500                 case SPU_CMD_END:
501  
502                     /* ff (end) */
503                     break;
504  
505                 default:
506  
507                     /* xx (unknown command) */
508                     msg_Err( p_spudec->p_fifo, "unknown command 0x%.2x",
509                                                i_command );
510                     return( 1 );
511             }
512
513             /* We need to check for quit commands here */
514             if( p_spudec->p_fifo->b_die )
515             {
516                 return( 1 );
517             }
518
519         } while( i_command != SPU_CMD_END );
520
521     } while( i_index == i_next_seq );
522
523     /* Check that the next sequence index matches the current one */
524     if( i_next_seq != i_cur_seq )
525     {
526         msg_Err( p_spudec->p_fifo, "index mismatch (0x%.4x != 0x%.4x)",
527                                    i_next_seq, i_cur_seq );
528         return( 1 );
529     }
530
531     if( i_index > p_spudec->i_spu_size )
532     {
533         msg_Err( p_spudec->p_fifo, "uh-oh, we went too far (0x%.4x > 0x%.4x)",
534                                    i_index, p_spudec->i_spu_size );
535         return( 1 );
536     }
537
538     if( !p_spu->i_start )
539     {
540         msg_Err( p_spudec->p_fifo, "no `start display' command" );
541     }
542
543     if( !p_spu->i_stop )
544     {
545         /* This subtitle will live for 5 seconds or until the next subtitle */
546         p_spu->i_stop = p_spu->i_start + 500 * 11000;
547         p_spu->b_ephemer = 1;
548     }
549
550     /* Get rid of padding bytes */
551     switch( p_spudec->i_spu_size - i_index )
552     {
553         /* Zero or one padding byte, quite usual */
554         case 1:
555             RemoveBits( &p_spudec->bit_stream, 8 );
556             i_index++;
557         case 0:
558             break;
559
560         /* More than one padding byte - this is very strange, but
561          * we can deal with it */
562         default:
563             msg_Warn( p_spudec->p_fifo,
564                       "%i padding bytes, we usually get 0 or 1 of them",
565                       p_spudec->i_spu_size - i_index );
566
567             while( i_index < p_spudec->i_spu_size )
568             {
569                 RemoveBits( &p_spudec->bit_stream, 8 );
570                 i_index++;
571             }
572
573             break;
574     }
575
576     if( b_force_display )
577     {
578         msg_Err( p_spudec->p_fifo, "\"force display\" command" );
579         msg_Err( p_spudec->p_fifo, "send mail to <sam@zoy.org> if you "
580                                    "want to help debugging this" );
581     }
582
583     /* Successfully parsed ! */
584     return( 0 );
585 }
586
587 /*****************************************************************************
588  * ParseRLE: parse the RLE part of the subtitle
589  *****************************************************************************
590  * This part parses the subtitle graphical data and stores it in a more
591  * convenient structure for later decoding. For more information on the
592  * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
593  *****************************************************************************/
594 static int ParseRLE( spudec_thread_t *p_spudec,
595                      subpicture_t * p_spu, u8 * p_src )
596 {
597     unsigned int i_code;
598
599     unsigned int i_width = p_spu->i_width;
600     unsigned int i_height = p_spu->i_height;
601     unsigned int i_x, i_y;
602
603     u16 *p_dest = (u16 *)p_spu->p_sys->p_data;
604
605     /* The subtitles are interlaced, we need two offsets */
606     unsigned int  i_id = 0;                   /* Start on the even SPU layer */
607     unsigned int  pi_table[ 2 ];
608     unsigned int *pi_offset;
609
610     vlc_bool_t b_empty_top = 1,
611                b_empty_bottom = 0;
612     unsigned int i_skipped_top = 0,
613                  i_skipped_bottom = 0;
614
615     /* Colormap statistics */
616     int i_border = -1;
617     int stats[4]; stats[0] = stats[1] = stats[2] = stats[3] = 0;
618
619     pi_table[ 0 ] = p_spu->p_sys->pi_offset[ 0 ] << 1;
620     pi_table[ 1 ] = p_spu->p_sys->pi_offset[ 1 ] << 1;
621
622     for( i_y = 0 ; i_y < i_height ; i_y++ )
623     {
624         pi_offset = pi_table + i_id;
625
626         for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
627         {
628             i_code = AddNibble( 0, p_src, pi_offset );
629
630             if( i_code < 0x04 )
631             {
632                 i_code = AddNibble( i_code, p_src, pi_offset );
633
634                 if( i_code < 0x10 )
635                 {
636                     i_code = AddNibble( i_code, p_src, pi_offset );
637
638                     if( i_code < 0x040 )
639                     {
640                         i_code = AddNibble( i_code, p_src, pi_offset );
641
642                         if( i_code < 0x0100 )
643                         {
644                             /* If the 14 first bits are set to 0, then it's a
645                              * new line. We emulate it. */
646                             if( i_code < 0x0004 )
647                             {
648                                 i_code |= ( i_width - i_x ) << 2;
649                             }
650                             else
651                             {
652                                 /* We have a boo boo ! */
653                                 msg_Err( p_spudec->p_fifo, "unknown RLE code "
654                                          "0x%.4x", i_code );
655                                 return( 1 );
656                             }
657                         }
658                     }
659                 }
660             }
661
662             if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
663             {
664                 msg_Err( p_spudec->p_fifo,
665                          "out of bounds, %i at (%i,%i) is out of %ix%i",
666                          i_code >> 2, i_x, i_y, i_width, i_height );
667                 return( 1 );
668             }
669
670             /* Try to find the border color */
671             if( p_spu->p_sys->pi_alpha[ i_code & 0x3 ] != 0x00 )
672             {
673                 i_border = i_code & 0x3;
674                 stats[i_border] += i_code >> 2;
675             }
676
677             if( (i_code >> 2) == i_width
678                  && p_spu->p_sys->pi_alpha[ i_code & 0x3 ] == 0x00 )
679             {
680                 if( b_empty_top )
681                 {
682                     /* This is a blank top line, we skip it */
683                     i_skipped_top++;
684                 }
685                 else
686                 {
687                     /* We can't be sure the current lines will be skipped,
688                      * so we store the code just in case. */
689                     *p_dest++ = i_code;
690
691                     b_empty_bottom = 1;
692                     i_skipped_bottom++;
693                 }
694             }
695             else
696             {
697                 /* We got a valid code, store it */
698                 *p_dest++ = i_code;
699
700                 /* Valid code means no blank line */
701                 b_empty_top = 0;
702                 b_empty_bottom = 0;
703                 i_skipped_bottom = 0;
704             }
705         }
706
707         /* Check that we didn't go too far */
708         if( i_x > i_width )
709         {
710             msg_Err( p_spudec->p_fifo, "i_x overflowed, %i > %i",
711                                        i_x, i_width );
712             return( 1 );
713         }
714
715         /* Byte-align the stream */
716         if( *pi_offset & 0x1 )
717         {
718             (*pi_offset)++;
719         }
720
721         /* Swap fields */
722         i_id = ~i_id & 0x1;
723     }
724
725     /* We shouldn't get any padding bytes */
726     if( i_y < i_height )
727     {
728         msg_Err( p_spudec->p_fifo, "padding bytes found in RLE sequence" );
729         msg_Err( p_spudec->p_fifo, "send mail to <sam@zoy.org> if you "
730                                    "want to help debugging this" );
731
732         /* Skip them just in case */
733         while( i_y < i_height )
734         {
735             *p_dest++ = i_width << 2;
736             i_y++;
737         }
738
739         return( 1 );
740     }
741
742     msg_Dbg( p_spudec->p_fifo, "valid subtitle, size: %ix%i, position: %i,%i",
743              p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
744
745     /* Crop if necessary */
746     if( i_skipped_top || i_skipped_bottom )
747     {
748         p_spu->i_y += i_skipped_top;
749         p_spu->i_height -= i_skipped_top + i_skipped_bottom;
750
751         msg_Dbg( p_spudec->p_fifo, "cropped to: %ix%i, position: %i,%i",
752                  p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
753     }
754
755     /* Handle color if no palette was found */
756     if( !p_spu->p_sys->b_palette )
757     {
758         int i, i_inner = -1, i_shade = -1;
759
760         /* Set the border color */
761         p_spu->p_sys->pi_yuv[i_border][0] = 0x00;
762         p_spu->p_sys->pi_yuv[i_border][1] = 0x80;
763         p_spu->p_sys->pi_yuv[i_border][2] = 0x80;
764         stats[i_border] = 0;
765
766         /* Find the inner colors */
767         for( i = 0 ; i < 4 && i_inner == -1 ; i++ )
768         {
769             if( stats[i] )
770             {
771                 i_inner = i;
772             }
773         }
774
775         for(       ; i < 4 && i_shade == -1 ; i++ )
776         {
777             if( stats[i] )
778             {
779                 if( stats[i] > stats[i_inner] )
780                 {
781                     i_shade = i_inner;
782                     i_inner = i;
783                 }
784                 else
785                 {
786                     i_shade = i;
787                 }
788             }
789         }
790
791         /* Set the inner color */
792         if( i_inner != -1 )
793         {
794             p_spu->p_sys->pi_yuv[i_inner][0] = 0xff;
795             p_spu->p_sys->pi_yuv[i_inner][1] = 0x80;
796             p_spu->p_sys->pi_yuv[i_inner][2] = 0x80;
797         }
798
799         /* Set the anti-aliasing color */
800         if( i_shade != -1 )
801         {
802             p_spu->p_sys->pi_yuv[i_shade][0] = 0x80;
803             p_spu->p_sys->pi_yuv[i_shade][1] = 0x80;
804             p_spu->p_sys->pi_yuv[i_shade][2] = 0x80;
805         }
806
807         msg_Dbg( p_spudec->p_fifo,
808                  "using custom palette (border %i, inner %i, shade %i)",
809                  i_border, i_inner, i_shade );
810     }
811
812     return( 0 );
813 }
814
815 /*****************************************************************************
816  * RenderSPU: draw an SPU on a picture
817  *****************************************************************************
818  * This is a fast implementation of the subpicture drawing code. The data
819  * has been preprocessed once, so we don't need to parse the RLE buffer again
820  * and again. Most sanity checks are already done so that this routine can be
821  * as fast as possible.
822  *****************************************************************************/
823 static void RenderSPU( vout_thread_t *p_vout, picture_t *p_pic,
824                        const subpicture_t *p_spu )
825 {
826     /* Common variables */
827     u16  p_clut16[4];
828     u32  p_clut32[4];
829     u8  *p_dest;
830     u16 *p_source = (u16 *)p_spu->p_sys->p_data;
831
832     int i_x, i_y;
833     int i_len, i_color;
834     u8  i_cnt;
835
836     /* RGB-specific */
837     int i_xscale, i_yscale, i_width, i_height, i_ytmp, i_yreal, i_ynext;
838
839     switch( p_vout->output.i_chroma )
840     {
841     /* I420 target, no scaling */
842     case FOURCC_I420:
843     case FOURCC_IYUV:
844     case FOURCC_YV12:
845
846     p_dest = p_pic->Y_PIXELS + p_spu->i_x + p_spu->i_width
847               + p_pic->Y_PITCH * ( p_spu->i_y + p_spu->i_height );
848
849     /* Draw until we reach the bottom of the subtitle */
850     for( i_y = p_spu->i_height * p_pic->Y_PITCH ;
851          i_y ;
852          i_y -= p_pic->Y_PITCH )
853     {
854         /* Draw until we reach the end of the line */
855         for( i_x = p_spu->i_width ; i_x ; )
856         {
857             /* Get the RLE part, then draw the line */
858             i_color = *p_source & 0x3;
859
860             switch( p_spu->p_sys->pi_alpha[ i_color ] )
861             {
862                 case 0x00:
863                     i_x -= *p_source++ >> 2;
864                     break;
865
866                 case 0x0f:
867                     i_len = *p_source++ >> 2;
868                     memset( p_dest - i_x - i_y,
869                             p_spu->p_sys->pi_yuv[i_color][0], i_len );
870                     i_x -= i_len;
871                     break;
872
873                 default:
874                     /* FIXME: we should do transparency */
875                     i_len = *p_source++ >> 2;
876                     memset( p_dest - i_x - i_y,
877                             p_spu->p_sys->pi_yuv[i_color][0], i_len );
878                     i_x -= i_len;
879                     break;
880             }
881         }
882     }
883
884     break;
885
886     /* RV16 target, scaling */
887     case FOURCC_RV16:
888
889     /* XXX: this is a COMPLETE HACK, memcpy is unable to do u16s anyway */
890     /* FIXME: get this from the DVD */
891     for( i_color = 0; i_color < 4; i_color++ )
892     {
893         p_clut16[i_color] = 0x1111
894                              * ( (u16)p_spu->p_sys->pi_yuv[i_color][0] >> 4 );
895     }
896
897     i_xscale = ( p_vout->output.i_width << 6 ) / p_vout->render.i_width;
898     i_yscale = ( p_vout->output.i_height << 6 ) / p_vout->render.i_height;
899
900     i_width  = p_spu->i_width  * i_xscale;
901     i_height = p_spu->i_height * i_yscale;
902
903     p_dest = p_pic->p->p_pixels + ( i_width >> 6 ) * 2
904               /* Add the picture coordinates and the SPU coordinates */
905               + ( (p_spu->i_x * i_xscale) >> 6 ) * 2
906               + ( (p_spu->i_y * i_yscale) >> 6 ) * p_pic->p->i_pitch;
907
908     /* Draw until we reach the bottom of the subtitle */
909     for( i_y = 0 ; i_y < i_height ; )
910     {
911         i_ytmp = i_y >> 6;
912         i_y += i_yscale;
913
914         /* Check whether we need to draw one line or more than one */
915         if( i_ytmp + 1 >= ( i_y >> 6 ) )
916         {
917             /* Just one line : we precalculate i_y >> 6 */
918             i_yreal = p_pic->p->i_pitch * i_ytmp;
919
920             /* Draw until we reach the end of the line */
921             for( i_x = i_width ; i_x ; )
922             {
923                 /* Get the RLE part, then draw the line */
924                 i_color = *p_source & 0x3;
925
926                 switch( p_spu->p_sys->pi_alpha[ i_color ] )
927                 {
928                 case 0x00:
929                     i_x -= i_xscale * ( *p_source++ >> 2 );
930                     break;
931
932                 case 0x0f:
933                     i_len = i_xscale * ( *p_source++ >> 2 );
934                     memset( p_dest - 2 * ( i_x >> 6 ) + i_yreal,
935                             p_clut16[ i_color ],
936                             2 * ( ( i_len >> 6 ) + 1 ) );
937                     i_x -= i_len;
938                     break;
939
940                 default:
941                     /* FIXME: we should do transparency */
942                     i_len = i_xscale * ( *p_source++ >> 2 );
943                     memset( p_dest - 2 * ( i_x >> 6 ) + i_yreal,
944                             p_clut16[ i_color ],
945                             2 * ( ( i_len >> 6 ) + 1 ) );
946                     i_x -= i_len;
947                     break;
948                 }
949
950             }
951         }
952         else
953         {
954             i_yreal = p_pic->p->i_pitch * i_ytmp;
955             i_ynext = p_pic->p->i_pitch * i_y >> 6;
956
957             /* Draw until we reach the end of the line */
958             for( i_x = i_width ; i_x ; )
959             {
960                 /* Get the RLE part, then draw as many lines as needed */
961                 i_color = *p_source & 0x3;
962
963                 switch( p_spu->p_sys->pi_alpha[ i_color ] )
964                 {
965                 case 0x00:
966                     i_x -= i_xscale * ( *p_source++ >> 2 );
967                     break;
968
969                 case 0x0f:
970                     i_len = i_xscale * ( *p_source++ >> 2 );
971                     for( i_ytmp = i_yreal ; i_ytmp < i_ynext ;
972                          i_ytmp += p_pic->p->i_pitch )
973                     {
974                         memset( p_dest - 2 * ( i_x >> 6 ) + i_ytmp,
975                                 p_clut16[ i_color ],
976                                 2 * ( ( i_len >> 6 ) + 1 ) );
977                     }
978                     i_x -= i_len;
979                     break;
980
981                 default:
982                     /* FIXME: we should do transparency */
983                     i_len = i_xscale * ( *p_source++ >> 2 );
984                     for( i_ytmp = i_yreal ; i_ytmp < i_ynext ;
985                          i_ytmp += p_pic->p->i_pitch )
986                     {
987                         memset( p_dest - 2 * ( i_x >> 6 ) + i_ytmp,
988                                 p_clut16[ i_color ],
989                                 2 * ( ( i_len >> 6 ) + 1 ) );
990                     }
991                     i_x -= i_len;
992                     break;
993                 }
994             }
995         }
996     }
997
998     break;
999
1000     /* RV32 target, scaling */
1001     case FOURCC_RV24:
1002     case FOURCC_RV32:
1003
1004     /* XXX: this is a COMPLETE HACK, memcpy is unable to do u32s anyway */
1005     /* FIXME: get this from the DVD */
1006     for( i_color = 0; i_color < 4; i_color++ )
1007     {
1008         p_clut32[i_color] = 0x11111111
1009                              * ( (u16)p_spu->p_sys->pi_yuv[i_color][0] >> 4 );
1010     }
1011
1012     i_xscale = ( p_vout->output.i_width << 6 ) / p_vout->render.i_width;
1013     i_yscale = ( p_vout->output.i_height << 6 ) / p_vout->render.i_height;
1014
1015     i_width  = p_spu->i_width  * i_xscale;
1016     i_height = p_spu->i_height * i_yscale;
1017
1018     p_dest = p_pic->p->p_pixels + ( i_width >> 6 ) * 4
1019               /* Add the picture coordinates and the SPU coordinates */
1020               + ( (p_spu->i_x * i_xscale) >> 6 ) * 4
1021               + ( (p_spu->i_y * i_yscale) >> 6 ) * p_pic->p->i_pitch;
1022
1023     /* Draw until we reach the bottom of the subtitle */
1024     for( i_y = 0 ; i_y < i_height ; )
1025     {
1026         i_ytmp = i_y >> 6;
1027         i_y += i_yscale;
1028
1029         /* Check whether we need to draw one line or more than one */
1030         if( i_ytmp + 1 >= ( i_y >> 6 ) )
1031         {
1032             /* Just one line : we precalculate i_y >> 6 */
1033             i_yreal = p_pic->p->i_pitch * i_ytmp;
1034
1035             /* Draw until we reach the end of the line */
1036             for( i_x = i_width ; i_x ; )
1037             {
1038                 /* Get the RLE part, then draw the line */
1039                 i_color = *p_source & 0x3;
1040
1041                 switch( p_spu->p_sys->pi_alpha[ i_color ] )
1042                 {
1043                 case 0x00:
1044                     i_x -= i_xscale * ( *p_source++ >> 2 );
1045                     break;
1046
1047                 case 0x0f:
1048                     i_len = i_xscale * ( *p_source++ >> 2 );
1049                     memset( p_dest - 4 * ( i_x >> 6 ) + i_yreal,
1050                             p_clut32[ i_color ], 4 * ( ( i_len >> 6 ) + 1 ) );
1051                     i_x -= i_len;
1052                     break;
1053
1054                 default:
1055                     /* FIXME: we should do transparency */
1056                     i_len = i_xscale * ( *p_source++ >> 2 );
1057                     memset( p_dest - 4 * ( i_x >> 6 ) + i_yreal,
1058                             p_clut32[ i_color ], 4 * ( ( i_len >> 6 ) + 1 ) );
1059                     i_x -= i_len;
1060                     break;
1061                 }
1062
1063             }
1064         }
1065         else
1066         {
1067             i_yreal = p_pic->p->i_pitch * i_ytmp;
1068             i_ynext = p_pic->p->i_pitch * i_y >> 6;
1069
1070             /* Draw until we reach the end of the line */
1071             for( i_x = i_width ; i_x ; )
1072             {
1073                 /* Get the RLE part, then draw as many lines as needed */
1074                 i_color = *p_source & 0x3;
1075
1076                 switch( p_spu->p_sys->pi_alpha[ i_color ] )
1077                 {
1078                 case 0x00:
1079                     i_x -= i_xscale * ( *p_source++ >> 2 );
1080                     break;
1081
1082                 case 0x0f:
1083                     i_len = i_xscale * ( *p_source++ >> 2 );
1084                     for( i_ytmp = i_yreal ; i_ytmp < i_ynext ;
1085                          i_ytmp += p_pic->p->i_pitch )
1086                     {
1087                         memset( p_dest - 4 * ( i_x >> 6 ) + i_ytmp,
1088                                 p_clut32[ i_color ],
1089                                 4 * ( ( i_len >> 6 ) + 1 ) );
1090                     }
1091                     i_x -= i_len;
1092                     break;
1093
1094                 default:
1095                     /* FIXME: we should do transparency */
1096                     i_len = i_xscale * ( *p_source++ >> 2 );
1097                     for( i_ytmp = i_yreal ; i_ytmp < i_ynext ;
1098                          i_ytmp += p_pic->p->i_pitch )
1099                     {
1100                         memset( p_dest - 4 * ( i_x >> 6 ) + i_ytmp,
1101                                 p_clut32[ i_color ],
1102                                 4 * ( ( i_len >> 6 ) + 1 ) );
1103                     }
1104                     i_x -= i_len;
1105                     break;
1106                 }
1107             }
1108         }
1109     }
1110
1111     break;
1112
1113     /* NVidia overlay, no scaling */
1114     case FOURCC_YUY2:
1115
1116     p_dest = p_pic->p->p_pixels +
1117               (p_spu->i_x + p_spu->i_width +
1118                p_vout->output.i_width * ( p_spu->i_y + p_spu->i_height )) * 2;
1119     /* Draw until we reach the bottom of the subtitle */
1120     for( i_y = p_spu->i_height * p_vout->output.i_width;
1121          i_y ;
1122          i_y -= p_vout->output.i_width )
1123     {
1124         /* Draw until we reach the end of the line */
1125         for( i_x = p_spu->i_width ; i_x ; )
1126         {
1127             /* Get the RLE part, then draw the line */
1128             i_color = *p_source & 0x3;
1129
1130             switch( p_spu->p_sys->pi_alpha[ i_color ] )
1131             {
1132             case 0x00:
1133                 i_x -= *p_source++ >> 2;
1134                 break;
1135
1136             case 0x0f:
1137                 i_len = *p_source++ >> 2;
1138                 for( i_cnt = 0; i_cnt < i_len; i_cnt++ )
1139                 {
1140                     /* draw a pixel */
1141                     /* Y */
1142                     memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2,
1143                             p_spu->p_sys->pi_yuv[i_color][0], 1);
1144
1145                     if (!(i_cnt & 0x01))
1146                     {
1147                         /* U and V */
1148                         memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2 + 1,
1149                                 0x80, 1);
1150                         memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2 + 3,
1151                                 0x80, 1);
1152                     }
1153                 }
1154                 i_x -= i_len;
1155                 break;
1156
1157             default:
1158                 /* FIXME: we should do transparency */
1159                 i_len = *p_source++ >> 2;
1160                 for( i_cnt = 0; i_cnt < i_len; i_cnt++ )
1161                 {
1162                     /* draw a pixel */
1163                     /* Y */
1164                     memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2,
1165                             p_spu->p_sys->pi_yuv[i_color][0], 1);
1166
1167                     if (!(i_cnt & 0x01))
1168                     {
1169                         /* U and V */
1170                         memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2 + 1,
1171                                 0x80, 1);
1172                         memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2 + 3,
1173                                 0x80, 1);
1174                     }
1175                 }
1176                 i_x -= i_len;
1177                 break;
1178             }
1179         }
1180     }
1181
1182     break;
1183
1184
1185     default:
1186         msg_Err( p_vout, "unknown chroma, can't render SPU" );
1187         break;
1188     }
1189 }