]> git.sesse.net Git - vlc/blob - plugins/spudec/spu_decoder.c
* ./plugins/dvd/dvd_es.c: error in selecting SPU stream.
[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.29 2002/06/27 19:46:32 sam 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, pi_alpha[4];
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                             /* FIXME: this job should be done sooner */
448 #ifndef WORDS_BIGENDIAN
449                             p_spu->p_sys->pi_yuv[3-i][0] = (i_color>>16) & 0xff;
450                             p_spu->p_sys->pi_yuv[3-i][1] = (i_color>>0) & 0xff;
451                             p_spu->p_sys->pi_yuv[3-i][2] = (i_color>>8) & 0xff;
452 #else
453                             p_spu->p_sys->pi_yuv[3-i][0] = (i_color>>8) & 0xff;
454                             p_spu->p_sys->pi_yuv[3-i][1] = (i_color>>24) & 0xff;
455                             p_spu->p_sys->pi_yuv[3-i][2] = (i_color>>16) & 0xff;
456 #endif
457                         }
458                     }
459                     else
460                     {
461                         RemoveBits( &p_spudec->bit_stream, 16 );
462                     }
463                     i_index += 2;
464  
465                     break;
466  
467                 case SPU_CMD_SET_ALPHACHANNEL:
468  
469                     /* 04xxxx (alpha channel) */
470                     pi_alpha[3] = GetBits( &p_spudec->bit_stream, 4 );
471                     pi_alpha[2] = GetBits( &p_spudec->bit_stream, 4 );
472                     pi_alpha[1] = GetBits( &p_spudec->bit_stream, 4 );
473                     pi_alpha[0] = GetBits( &p_spudec->bit_stream, 4 );
474
475                     /* Ignore blank alpha palette. Sometimes spurious blank
476                      * alpha palettes are present - dunno why. */
477                     if( pi_alpha[0] | pi_alpha[1] | pi_alpha[2] | pi_alpha[3] )
478                     {
479                         p_spu->p_sys->pi_alpha[0] = pi_alpha[0];
480                         p_spu->p_sys->pi_alpha[1] = pi_alpha[1];
481                         p_spu->p_sys->pi_alpha[2] = pi_alpha[2];
482                         p_spu->p_sys->pi_alpha[3] = pi_alpha[3];
483                     }
484                     else
485                     {
486                         msg_Warn( p_spudec->p_fifo,
487                                   "ignoring blank alpha palette" );
488                     }
489
490                     i_index += 2;
491  
492                     break;
493  
494                 case SPU_CMD_SET_COORDINATES:
495  
496                     /* 05xxxyyyxxxyyy (coordinates) */
497                     p_spu->i_x = GetBits( &p_spudec->bit_stream, 12 );
498                     p_spu->i_width = GetBits( &p_spudec->bit_stream, 12 )
499                                       - p_spu->i_x + 1;
500  
501                     p_spu->i_y = GetBits( &p_spudec->bit_stream, 12 );
502                     p_spu->i_height = GetBits( &p_spudec->bit_stream, 12 )
503                                        - p_spu->i_y + 1;
504  
505                     i_index += 6;
506  
507                     break;
508  
509                 case SPU_CMD_SET_OFFSETS:
510  
511                     /* 06xxxxyyyy (byte offsets) */
512                     p_spu->p_sys->pi_offset[0] =
513                         GetBits( &p_spudec->bit_stream, 16 ) - 4;
514  
515                     p_spu->p_sys->pi_offset[1] =
516                         GetBits( &p_spudec->bit_stream, 16 ) - 4;
517  
518                     i_index += 4;
519  
520                     break;
521  
522                 case SPU_CMD_END:
523  
524                     /* ff (end) */
525                     break;
526  
527                 default:
528  
529                     /* xx (unknown command) */
530                     msg_Err( p_spudec->p_fifo, "unknown command 0x%.2x",
531                                                i_command );
532                     return( 1 );
533             }
534
535             /* We need to check for quit commands here */
536             if( p_spudec->p_fifo->b_die )
537             {
538                 return( 1 );
539             }
540
541         } while( i_command != SPU_CMD_END );
542
543     } while( i_index == i_next_seq );
544
545     /* Check that the next sequence index matches the current one */
546     if( i_next_seq != i_cur_seq )
547     {
548         msg_Err( p_spudec->p_fifo, "index mismatch (0x%.4x != 0x%.4x)",
549                                    i_next_seq, i_cur_seq );
550         return( 1 );
551     }
552
553     if( i_index > p_spudec->i_spu_size )
554     {
555         msg_Err( p_spudec->p_fifo, "uh-oh, we went too far (0x%.4x > 0x%.4x)",
556                                    i_index, p_spudec->i_spu_size );
557         return( 1 );
558     }
559
560     if( !p_spu->i_start )
561     {
562         msg_Err( p_spudec->p_fifo, "no `start display' command" );
563     }
564
565     if( !p_spu->i_stop )
566     {
567         /* This subtitle will live for 5 seconds or until the next subtitle */
568         p_spu->i_stop = p_spu->i_start + 500 * 11000;
569         p_spu->b_ephemer = 1;
570     }
571
572     /* Get rid of padding bytes */
573     switch( p_spudec->i_spu_size - i_index )
574     {
575         /* Zero or one padding byte, quite usual */
576         case 1:
577             RemoveBits( &p_spudec->bit_stream, 8 );
578             i_index++;
579         case 0:
580             break;
581
582         /* More than one padding byte - this is very strange, but
583          * we can deal with it */
584         default:
585             msg_Warn( p_spudec->p_fifo,
586                       "%i padding bytes, we usually get 0 or 1 of them",
587                       p_spudec->i_spu_size - i_index );
588
589             while( i_index < p_spudec->i_spu_size )
590             {
591                 RemoveBits( &p_spudec->bit_stream, 8 );
592                 i_index++;
593             }
594
595             break;
596     }
597
598     if( b_force_display )
599     {
600         msg_Err( p_spudec->p_fifo, "\"force display\" command" );
601         msg_Err( p_spudec->p_fifo, "send mail to <sam@zoy.org> if you "
602                                    "want to help debugging this" );
603     }
604
605     /* Successfully parsed ! */
606     return( 0 );
607 }
608
609 /*****************************************************************************
610  * ParseRLE: parse the RLE part of the subtitle
611  *****************************************************************************
612  * This part parses the subtitle graphical data and stores it in a more
613  * convenient structure for later decoding. For more information on the
614  * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
615  *****************************************************************************/
616 static int ParseRLE( spudec_thread_t *p_spudec,
617                      subpicture_t * p_spu, u8 * p_src )
618 {
619     unsigned int i_code;
620
621     unsigned int i_width = p_spu->i_width;
622     unsigned int i_height = p_spu->i_height;
623     unsigned int i_x, i_y;
624
625     u16 *p_dest = (u16 *)p_spu->p_sys->p_data;
626
627     /* The subtitles are interlaced, we need two offsets */
628     unsigned int  i_id = 0;                   /* Start on the even SPU layer */
629     unsigned int  pi_table[ 2 ];
630     unsigned int *pi_offset;
631
632     vlc_bool_t b_empty_top = 1,
633                b_empty_bottom = 0;
634     unsigned int i_skipped_top = 0,
635                  i_skipped_bottom = 0;
636
637     /* Colormap statistics */
638     int i_border = -1;
639     int stats[4]; stats[0] = stats[1] = stats[2] = stats[3] = 0;
640
641     pi_table[ 0 ] = p_spu->p_sys->pi_offset[ 0 ] << 1;
642     pi_table[ 1 ] = p_spu->p_sys->pi_offset[ 1 ] << 1;
643
644     for( i_y = 0 ; i_y < i_height ; i_y++ )
645     {
646         pi_offset = pi_table + i_id;
647
648         for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
649         {
650             i_code = AddNibble( 0, p_src, pi_offset );
651
652             if( i_code < 0x04 )
653             {
654                 i_code = AddNibble( i_code, p_src, pi_offset );
655
656                 if( i_code < 0x10 )
657                 {
658                     i_code = AddNibble( i_code, p_src, pi_offset );
659
660                     if( i_code < 0x040 )
661                     {
662                         i_code = AddNibble( i_code, p_src, pi_offset );
663
664                         if( i_code < 0x0100 )
665                         {
666                             /* If the 14 first bits are set to 0, then it's a
667                              * new line. We emulate it. */
668                             if( i_code < 0x0004 )
669                             {
670                                 i_code |= ( i_width - i_x ) << 2;
671                             }
672                             else
673                             {
674                                 /* We have a boo boo ! */
675                                 msg_Err( p_spudec->p_fifo, "unknown RLE code "
676                                          "0x%.4x", i_code );
677                                 return( 1 );
678                             }
679                         }
680                     }
681                 }
682             }
683
684             if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
685             {
686                 msg_Err( p_spudec->p_fifo,
687                          "out of bounds, %i at (%i,%i) is out of %ix%i",
688                          i_code >> 2, i_x, i_y, i_width, i_height );
689                 return( 1 );
690             }
691
692             /* Try to find the border color */
693             if( p_spu->p_sys->pi_alpha[ i_code & 0x3 ] != 0x00 )
694             {
695                 i_border = i_code & 0x3;
696                 stats[i_border] += i_code >> 2;
697             }
698
699             if( (i_code >> 2) == i_width
700                  && p_spu->p_sys->pi_alpha[ i_code & 0x3 ] == 0x00 )
701             {
702                 if( b_empty_top )
703                 {
704                     /* This is a blank top line, we skip it */
705                     i_skipped_top++;
706                 }
707                 else
708                 {
709                     /* We can't be sure the current lines will be skipped,
710                      * so we store the code just in case. */
711                     *p_dest++ = i_code;
712
713                     b_empty_bottom = 1;
714                     i_skipped_bottom++;
715                 }
716             }
717             else
718             {
719                 /* We got a valid code, store it */
720                 *p_dest++ = i_code;
721
722                 /* Valid code means no blank line */
723                 b_empty_top = 0;
724                 b_empty_bottom = 0;
725                 i_skipped_bottom = 0;
726             }
727         }
728
729         /* Check that we didn't go too far */
730         if( i_x > i_width )
731         {
732             msg_Err( p_spudec->p_fifo, "i_x overflowed, %i > %i",
733                                        i_x, i_width );
734             return( 1 );
735         }
736
737         /* Byte-align the stream */
738         if( *pi_offset & 0x1 )
739         {
740             (*pi_offset)++;
741         }
742
743         /* Swap fields */
744         i_id = ~i_id & 0x1;
745     }
746
747     /* We shouldn't get any padding bytes */
748     if( i_y < i_height )
749     {
750         msg_Err( p_spudec->p_fifo, "padding bytes found in RLE sequence" );
751         msg_Err( p_spudec->p_fifo, "send mail to <sam@zoy.org> if you "
752                                    "want to help debugging this" );
753
754         /* Skip them just in case */
755         while( i_y < i_height )
756         {
757             *p_dest++ = i_width << 2;
758             i_y++;
759         }
760
761         return( 1 );
762     }
763
764     msg_Dbg( p_spudec->p_fifo, "valid subtitle, size: %ix%i, position: %i,%i",
765              p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
766
767     /* Crop if necessary */
768     if( i_skipped_top || i_skipped_bottom )
769     {
770         p_spu->i_y += i_skipped_top;
771         p_spu->i_height -= i_skipped_top + i_skipped_bottom;
772
773         msg_Dbg( p_spudec->p_fifo, "cropped to: %ix%i, position: %i,%i",
774                  p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
775     }
776
777     /* Handle color if no palette was found */
778     if( !p_spu->p_sys->b_palette )
779     {
780         int i, i_inner = -1, i_shade = -1;
781
782         /* Set the border color */
783         p_spu->p_sys->pi_yuv[i_border][0] = 0x00;
784         p_spu->p_sys->pi_yuv[i_border][1] = 0x80;
785         p_spu->p_sys->pi_yuv[i_border][2] = 0x80;
786         stats[i_border] = 0;
787
788         /* Find the inner colors */
789         for( i = 0 ; i < 4 && i_inner == -1 ; i++ )
790         {
791             if( stats[i] )
792             {
793                 i_inner = i;
794             }
795         }
796
797         for(       ; i < 4 && i_shade == -1 ; i++ )
798         {
799             if( stats[i] )
800             {
801                 if( stats[i] > stats[i_inner] )
802                 {
803                     i_shade = i_inner;
804                     i_inner = i;
805                 }
806                 else
807                 {
808                     i_shade = i;
809                 }
810             }
811         }
812
813         /* Set the inner color */
814         if( i_inner != -1 )
815         {
816             p_spu->p_sys->pi_yuv[i_inner][0] = 0xff;
817             p_spu->p_sys->pi_yuv[i_inner][1] = 0x80;
818             p_spu->p_sys->pi_yuv[i_inner][2] = 0x80;
819         }
820
821         /* Set the anti-aliasing color */
822         if( i_shade != -1 )
823         {
824             p_spu->p_sys->pi_yuv[i_shade][0] = 0x80;
825             p_spu->p_sys->pi_yuv[i_shade][1] = 0x80;
826             p_spu->p_sys->pi_yuv[i_shade][2] = 0x80;
827         }
828
829         msg_Dbg( p_spudec->p_fifo,
830                  "using custom palette (border %i, inner %i, shade %i)",
831                  i_border, i_inner, i_shade );
832     }
833
834     return( 0 );
835 }
836
837 /*****************************************************************************
838  * RenderSPU: draw an SPU on a picture
839  *****************************************************************************
840  * This is a fast implementation of the subpicture drawing code. The data
841  * has been preprocessed once, so we don't need to parse the RLE buffer again
842  * and again. Most sanity checks are already done so that this routine can be
843  * as fast as possible.
844  *****************************************************************************/
845 static void RenderSPU( vout_thread_t *p_vout, picture_t *p_pic,
846                        const subpicture_t *p_spu )
847 {
848     /* Common variables */
849     u16  p_clut16[4];
850     u32  p_clut32[4];
851     u8  *p_dest;
852     u8  *p_destptr = (u8 *)p_dest;
853     u16 *p_source = (u16 *)p_spu->p_sys->p_data;
854
855     int i_x, i_y;
856     int i_len, i_color, i_colprecomp, i_destalpha;
857     u8  i_cnt;
858
859     /* RGB-specific */
860     int i_xscale, i_yscale, i_width, i_height, i_ytmp, i_yreal, i_ynext;
861
862     switch( p_vout->output.i_chroma )
863     {
864     /* I420 target, no scaling */
865     case FOURCC_I420:
866     case FOURCC_IYUV:
867     case FOURCC_YV12:
868
869     p_dest = p_pic->Y_PIXELS + p_spu->i_x + p_spu->i_width
870               + p_pic->Y_PITCH * ( p_spu->i_y + p_spu->i_height );
871
872     /* Draw until we reach the bottom of the subtitle */
873     for( i_y = p_spu->i_height * p_pic->Y_PITCH ;
874          i_y ;
875          i_y -= p_pic->Y_PITCH )
876     {
877         /* Draw until we reach the end of the line */
878         for( i_x = p_spu->i_width ; i_x ; )
879         {
880             /* Get the RLE part, then draw the line */
881             i_color = *p_source & 0x3;
882             i_len = *p_source++ >> 2;
883
884             switch( p_spu->p_sys->pi_alpha[ i_color ] )
885             {
886                 case 0x00:
887                     break;
888
889                 case 0x0f:
890                     memset( p_dest - i_x - i_y,
891                             p_spu->p_sys->pi_yuv[i_color][0], i_len );
892                     break;
893
894                 default:
895                     /* To be able to divide by 16 (>>4) we add 1 to the alpha.
896                      * This means Alpha 0 won't be completely transparent, but
897                      * that's handled in a special case above anyway. */
898                     i_colprecomp = p_spu->p_sys->pi_yuv[i_color][0]
899                                     * (p_spu->p_sys->pi_alpha[ i_color ] + 1);
900                     i_destalpha = 15 - p_spu->p_sys->pi_alpha[ i_color ];
901
902                     for ( p_destptr = p_dest - i_x - i_y;
903                           p_destptr < p_dest - i_x - i_y + i_len;
904                           p_destptr++ )
905                     {
906                         *p_destptr = ( i_colprecomp +
907                                         *p_destptr * i_destalpha ) >> 4;
908                     }
909                     break;
910
911             }
912             i_x -= i_len;
913         }
914     }
915
916     break;
917
918     /* RV16 target, scaling */
919     case FOURCC_RV16:
920
921     /* XXX: this is a COMPLETE HACK, memcpy is unable to do u16s anyway */
922     /* FIXME: get this from the DVD */
923     for( i_color = 0; i_color < 4; i_color++ )
924     {
925         p_clut16[i_color] = 0x1111
926                              * ( (u16)p_spu->p_sys->pi_yuv[i_color][0] >> 4 );
927     }
928
929     i_xscale = ( p_vout->output.i_width << 6 ) / p_vout->render.i_width;
930     i_yscale = ( p_vout->output.i_height << 6 ) / p_vout->render.i_height;
931
932     i_width  = p_spu->i_width  * i_xscale;
933     i_height = p_spu->i_height * i_yscale;
934
935     p_dest = p_pic->p->p_pixels + ( i_width >> 6 ) * 2
936               /* Add the picture coordinates and the SPU coordinates */
937               + ( (p_spu->i_x * i_xscale) >> 6 ) * 2
938               + ( (p_spu->i_y * i_yscale) >> 6 ) * p_pic->p->i_pitch;
939
940     /* Draw until we reach the bottom of the subtitle */
941     for( i_y = 0 ; i_y < i_height ; )
942     {
943         i_ytmp = i_y >> 6;
944         i_y += i_yscale;
945
946         /* Check whether we need to draw one line or more than one */
947         if( i_ytmp + 1 >= ( i_y >> 6 ) )
948         {
949             /* Just one line : we precalculate i_y >> 6 */
950             i_yreal = p_pic->p->i_pitch * i_ytmp;
951
952             /* Draw until we reach the end of the line */
953             for( i_x = i_width ; i_x ; )
954             {
955                 /* Get the RLE part, then draw the line */
956                 i_color = *p_source & 0x3;
957
958                 switch( p_spu->p_sys->pi_alpha[ i_color ] )
959                 {
960                 case 0x00:
961                     i_x -= i_xscale * ( *p_source++ >> 2 );
962                     break;
963
964                 case 0x0f:
965                     i_len = i_xscale * ( *p_source++ >> 2 );
966                     memset( p_dest - 2 * ( i_x >> 6 ) + i_yreal,
967                             p_clut16[ i_color ],
968                             2 * ( ( i_len >> 6 ) + 1 ) );
969                     i_x -= i_len;
970                     break;
971
972                 default:
973                     /* FIXME: we should do transparency */
974                     i_len = i_xscale * ( *p_source++ >> 2 );
975                     memset( p_dest - 2 * ( i_x >> 6 ) + i_yreal,
976                             p_clut16[ i_color ],
977                             2 * ( ( i_len >> 6 ) + 1 ) );
978                     i_x -= i_len;
979                     break;
980                 }
981
982             }
983         }
984         else
985         {
986             i_yreal = p_pic->p->i_pitch * i_ytmp;
987             i_ynext = p_pic->p->i_pitch * i_y >> 6;
988
989             /* Draw until we reach the end of the line */
990             for( i_x = i_width ; i_x ; )
991             {
992                 /* Get the RLE part, then draw as many lines as needed */
993                 i_color = *p_source & 0x3;
994
995                 switch( p_spu->p_sys->pi_alpha[ i_color ] )
996                 {
997                 case 0x00:
998                     i_x -= i_xscale * ( *p_source++ >> 2 );
999                     break;
1000
1001                 case 0x0f:
1002                     i_len = i_xscale * ( *p_source++ >> 2 );
1003                     for( i_ytmp = i_yreal ; i_ytmp < i_ynext ;
1004                          i_ytmp += p_pic->p->i_pitch )
1005                     {
1006                         memset( p_dest - 2 * ( i_x >> 6 ) + i_ytmp,
1007                                 p_clut16[ i_color ],
1008                                 2 * ( ( i_len >> 6 ) + 1 ) );
1009                     }
1010                     i_x -= i_len;
1011                     break;
1012
1013                 default:
1014                     /* FIXME: we should do transparency */
1015                     i_len = i_xscale * ( *p_source++ >> 2 );
1016                     for( i_ytmp = i_yreal ; i_ytmp < i_ynext ;
1017                          i_ytmp += p_pic->p->i_pitch )
1018                     {
1019                         memset( p_dest - 2 * ( i_x >> 6 ) + i_ytmp,
1020                                 p_clut16[ i_color ],
1021                                 2 * ( ( i_len >> 6 ) + 1 ) );
1022                     }
1023                     i_x -= i_len;
1024                     break;
1025                 }
1026             }
1027         }
1028     }
1029
1030     break;
1031
1032     /* RV32 target, scaling */
1033     case FOURCC_RV24:
1034     case FOURCC_RV32:
1035
1036     /* XXX: this is a COMPLETE HACK, memcpy is unable to do u32s anyway */
1037     /* FIXME: get this from the DVD */
1038     for( i_color = 0; i_color < 4; i_color++ )
1039     {
1040         p_clut32[i_color] = 0x11111111
1041                              * ( (u16)p_spu->p_sys->pi_yuv[i_color][0] >> 4 );
1042     }
1043
1044     i_xscale = ( p_vout->output.i_width << 6 ) / p_vout->render.i_width;
1045     i_yscale = ( p_vout->output.i_height << 6 ) / p_vout->render.i_height;
1046
1047     i_width  = p_spu->i_width  * i_xscale;
1048     i_height = p_spu->i_height * i_yscale;
1049
1050     p_dest = p_pic->p->p_pixels + ( i_width >> 6 ) * 4
1051               /* Add the picture coordinates and the SPU coordinates */
1052               + ( (p_spu->i_x * i_xscale) >> 6 ) * 4
1053               + ( (p_spu->i_y * i_yscale) >> 6 ) * p_pic->p->i_pitch;
1054
1055     /* Draw until we reach the bottom of the subtitle */
1056     for( i_y = 0 ; i_y < i_height ; )
1057     {
1058         i_ytmp = i_y >> 6;
1059         i_y += i_yscale;
1060
1061         /* Check whether we need to draw one line or more than one */
1062         if( i_ytmp + 1 >= ( i_y >> 6 ) )
1063         {
1064             /* Just one line : we precalculate i_y >> 6 */
1065             i_yreal = p_pic->p->i_pitch * i_ytmp;
1066
1067             /* Draw until we reach the end of the line */
1068             for( i_x = i_width ; i_x ; )
1069             {
1070                 /* Get the RLE part, then draw the line */
1071                 i_color = *p_source & 0x3;
1072
1073                 switch( p_spu->p_sys->pi_alpha[ i_color ] )
1074                 {
1075                 case 0x00:
1076                     i_x -= i_xscale * ( *p_source++ >> 2 );
1077                     break;
1078
1079                 case 0x0f:
1080                     i_len = i_xscale * ( *p_source++ >> 2 );
1081                     memset( p_dest - 4 * ( i_x >> 6 ) + i_yreal,
1082                             p_clut32[ i_color ], 4 * ( ( i_len >> 6 ) + 1 ) );
1083                     i_x -= i_len;
1084                     break;
1085
1086                 default:
1087                     /* FIXME: we should do transparency */
1088                     i_len = i_xscale * ( *p_source++ >> 2 );
1089                     memset( p_dest - 4 * ( i_x >> 6 ) + i_yreal,
1090                             p_clut32[ i_color ], 4 * ( ( i_len >> 6 ) + 1 ) );
1091                     i_x -= i_len;
1092                     break;
1093                 }
1094
1095             }
1096         }
1097         else
1098         {
1099             i_yreal = p_pic->p->i_pitch * i_ytmp;
1100             i_ynext = p_pic->p->i_pitch * i_y >> 6;
1101
1102             /* Draw until we reach the end of the line */
1103             for( i_x = i_width ; i_x ; )
1104             {
1105                 /* Get the RLE part, then draw as many lines as needed */
1106                 i_color = *p_source & 0x3;
1107
1108                 switch( p_spu->p_sys->pi_alpha[ i_color ] )
1109                 {
1110                 case 0x00:
1111                     i_x -= i_xscale * ( *p_source++ >> 2 );
1112                     break;
1113
1114                 case 0x0f:
1115                     i_len = i_xscale * ( *p_source++ >> 2 );
1116                     for( i_ytmp = i_yreal ; i_ytmp < i_ynext ;
1117                          i_ytmp += p_pic->p->i_pitch )
1118                     {
1119                         memset( p_dest - 4 * ( i_x >> 6 ) + i_ytmp,
1120                                 p_clut32[ i_color ],
1121                                 4 * ( ( i_len >> 6 ) + 1 ) );
1122                     }
1123                     i_x -= i_len;
1124                     break;
1125
1126                 default:
1127                     /* FIXME: we should do transparency */
1128                     i_len = i_xscale * ( *p_source++ >> 2 );
1129                     for( i_ytmp = i_yreal ; i_ytmp < i_ynext ;
1130                          i_ytmp += p_pic->p->i_pitch )
1131                     {
1132                         memset( p_dest - 4 * ( i_x >> 6 ) + i_ytmp,
1133                                 p_clut32[ i_color ],
1134                                 4 * ( ( i_len >> 6 ) + 1 ) );
1135                     }
1136                     i_x -= i_len;
1137                     break;
1138                 }
1139             }
1140         }
1141     }
1142
1143     break;
1144
1145     /* NVidia overlay, no scaling */
1146     case FOURCC_YUY2:
1147
1148     p_dest = p_pic->p->p_pixels +
1149               (p_spu->i_x + p_spu->i_width +
1150                p_vout->output.i_width * ( p_spu->i_y + p_spu->i_height )) * 2;
1151     /* Draw until we reach the bottom of the subtitle */
1152     for( i_y = p_spu->i_height * p_vout->output.i_width;
1153          i_y ;
1154          i_y -= p_vout->output.i_width )
1155     {
1156         /* Draw until we reach the end of the line */
1157         for( i_x = p_spu->i_width ; i_x ; )
1158         {
1159             /* Get the RLE part, then draw the line */
1160             i_color = *p_source & 0x3;
1161
1162             switch( p_spu->p_sys->pi_alpha[ i_color ] )
1163             {
1164             case 0x00:
1165                 i_x -= *p_source++ >> 2;
1166                 break;
1167
1168             case 0x0f:
1169                 i_len = *p_source++ >> 2;
1170                 for( i_cnt = 0; i_cnt < i_len; i_cnt++ )
1171                 {
1172                     /* draw a pixel */
1173                     /* Y */
1174                     memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2,
1175                             p_spu->p_sys->pi_yuv[i_color][0], 1);
1176
1177                     if (!(i_cnt & 0x01))
1178                     {
1179                         /* U and V */
1180                         memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2 + 1,
1181                                 0x80, 1);
1182                         memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2 + 3,
1183                                 0x80, 1);
1184                     }
1185                 }
1186                 i_x -= i_len;
1187                 break;
1188
1189             default:
1190                 /* FIXME: we should do transparency */
1191                 i_len = *p_source++ >> 2;
1192                 for( i_cnt = 0; i_cnt < i_len; i_cnt++ )
1193                 {
1194                     /* draw a pixel */
1195                     /* Y */
1196                     memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2,
1197                             p_spu->p_sys->pi_yuv[i_color][0], 1);
1198
1199                     if (!(i_cnt & 0x01))
1200                     {
1201                         /* U and V */
1202                         memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2 + 1,
1203                                 0x80, 1);
1204                         memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2 + 3,
1205                                 0x80, 1);
1206                     }
1207                 }
1208                 i_x -= i_len;
1209                 break;
1210             }
1211         }
1212     }
1213
1214     break;
1215
1216
1217     default:
1218         msg_Err( p_vout, "unknown chroma, can't render SPU" );
1219         break;
1220     }
1221 }