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