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 $
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Rudolf Cornelissen <rag.cornelissen@inter.nl.net>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
29 #include <string.h> /* memcpy(), memset() */
33 #include <vlc/decoder.h>
36 # include <unistd.h> /* getpid() */
39 #ifdef WIN32 /* getpid() for win32 is located in process.h */
43 #include "spu_decoder.h"
45 /*****************************************************************************
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 * );
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 * );
60 /*****************************************************************************
62 *****************************************************************************/
63 void _M( spudec_getfunctions )( function_list_t * p_function_list )
65 p_function_list->functions.dec.pf_probe = decoder_Probe;
66 p_function_list->functions.dec.pf_run = decoder_Run;
69 /*****************************************************************************
70 * Build configuration tree.
71 *****************************************************************************/
76 SET_DESCRIPTION( _("DVD subtitles decoder module") )
77 ADD_CAPABILITY( DECODER, 50 )
81 _M( spudec_getfunctions )( &p_module->p_functions->dec );
84 MODULE_DEACTIVATE_START
85 MODULE_DEACTIVATE_STOP
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
92 *****************************************************************************/
93 static int decoder_Probe( u8 *pi_type )
95 return ( *pi_type == DVD_SPU_ES ) ? 0 : -1;
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 )
103 spudec_thread_t * p_spudec;
105 /* Allocate the memory needed to store the thread's structure */
106 p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) );
108 if ( p_spudec == NULL )
110 msg_Err( p_fifo, "out of memory" );
111 DecoderError( p_fifo );
116 * Initialize the thread properties
118 p_spudec->p_vout = NULL;
119 p_spudec->p_fifo = p_fifo;
122 * Initialize thread and free configuration
124 p_spudec->p_fifo->b_error = InitThread( p_spudec );
127 * Main loop - it is not executed if an error occured during
130 while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
132 if( !SyncPacket( p_spudec ) )
134 ParsePacket( p_spudec );
141 if( p_spudec->p_fifo->b_error )
143 DecoderError( p_spudec->p_fifo );
146 EndThread( p_spudec );
151 EndThread( p_spudec );
155 /* following functions are local */
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 )
166 /* Find an available video output */
169 if( p_spudec->p_fifo->b_die || p_spudec->p_fifo->b_error )
174 p_spudec->p_vout = vlc_object_find( p_spudec->p_fifo, VLC_OBJECT_VOUT,
177 if( p_spudec->p_vout )
182 msleep( VOUT_OUTMEM_SLEEP );
186 InitBitstream( &p_spudec->bit_stream, p_spudec->p_fifo, NULL, NULL );
188 /* Mark thread as running and return */
192 /*****************************************************************************
193 * EndThread: thread destruction
194 *****************************************************************************
195 * This function is called when the thread ends after a sucessful
197 *****************************************************************************/
198 static void EndThread( spudec_thread_t *p_spudec )
200 if( p_spudec->p_vout != NULL
201 && p_spudec->p_vout->p_subpicture != NULL )
203 subpicture_t * p_subpic;
206 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
208 p_subpic = &p_spudec->p_vout->p_subpicture[i_subpic];
210 if( p_subpic != NULL &&
211 ( ( p_subpic->i_status == RESERVED_SUBPICTURE )
212 || ( p_subpic->i_status == READY_SUBPICTURE ) ) )
214 vout_DestroySubPicture( p_spudec->p_vout, p_subpic );
218 vlc_object_release( p_spudec->p_vout );
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 )
232 /* Re-align the buffer on an 8-bit boundary */
233 RealignBits( &p_spudec->bit_stream );
235 /* The total SPU packet size, often bigger than a PS packet */
236 p_spudec->i_spu_size = GetBits( &p_spudec->bit_stream, 16 );
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;
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 ) )
248 RemoveBits( &p_spudec->bit_stream, 16 );
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
258 *****************************************************************************/
259 static void ParsePacket( spudec_thread_t *p_spudec )
261 subpicture_t * p_spu;
263 unsigned int i_offset;
265 msg_Dbg( p_spudec->p_fifo, "trying to gather a 0x%.2x long subtitle",
266 p_spudec->i_spu_size );
268 /* We cannot display a subpicture with no date */
269 if( p_spudec->p_fifo->p_first->i_pts == 0 )
271 msg_Warn( p_spudec->p_fifo, "subtitle without a date" );
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. */
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;
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;
297 /* Allocate the temporary buffer we will parse */
298 p_src = malloc( p_spudec->i_rle_size );
302 msg_Err( p_spudec->p_fifo, "out of memory" );
303 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
308 for( i_offset = 0; i_offset < p_spudec->i_rle_size;
309 i_offset += SPU_CHUNK_SIZE )
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 );
315 /* Abort subtitle parsing if we were requested to stop */
316 if( p_spudec->p_fifo->b_die )
319 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
325 /* Dump the subtitle info */
326 intf_WarnHexDump( 5, p_spu->p_sys->p_data, p_spudec->i_rle_size );
329 /* Getting the control part */
330 if( ParseControlSequences( p_spudec, p_spu ) )
332 /* There was a parse error, delete the subpicture */
334 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
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. */
342 if( ParseRLE( p_spudec, p_spu, p_src ) )
344 /* There was a parse error, delete the subpicture */
346 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
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] );
354 /* SPU is finished - we can ask the video output to display it */
355 vout_DisplaySubPicture( p_spudec->p_vout, p_spu );
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 )
371 /* Our current index in the SPU packet */
372 int i_index = p_spudec->i_rle_size + 4;
374 /* The next start-of-control-sequence index and the previous one */
375 int i_next_seq, i_cur_seq;
377 /* Command time and date */
383 /* XXX: temporary variables */
384 vlc_bool_t b_force_display = 0;
386 /* Initialize the structure */
387 p_spu->i_start = p_spu->i_stop = 0;
388 p_spu->b_ephemer = 0;
392 /* Get the control sequence date */
393 i_date = GetBits( &p_spudec->bit_stream, 16 );
397 i_next_seq = GetBits( &p_spudec->bit_stream, 16 );
399 /* Skip what we just read */
404 i_command = GetBits( &p_spudec->bit_stream, 8 );
409 case SPU_CMD_FORCE_DISPLAY:
411 /* 00 (force displaying) */
412 p_spu->i_start = p_spu->p_sys->i_pts + ( i_date * 11000 );
417 /* Convert the dates in seconds to PTS values */
418 case SPU_CMD_START_DISPLAY:
420 /* 01 (start displaying) */
421 p_spu->i_start = p_spu->p_sys->i_pts + ( i_date * 11000 );
425 case SPU_CMD_STOP_DISPLAY:
427 /* 02 (stop displaying) */
428 p_spu->i_stop = p_spu->p_sys->i_pts + ( i_date * 11000 );
432 case SPU_CMD_SET_PALETTE:
434 /* 03xxxx (palette) */
435 if( p_spudec->p_fifo->p_demux_data &&
436 *(int*)p_spudec->p_fifo->p_demux_data == 0xBeeF )
440 p_spu->p_sys->b_palette = 1;
441 for( i = 0; i < 4 ; i++ )
443 i_color = ((u32*)((char*)p_spudec->p_fifo->
444 p_demux_data + sizeof(int)))[
445 GetBits(&p_spudec->bit_stream, 4) ];
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;
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;
461 RemoveBits( &p_spudec->bit_stream, 16 );
467 case SPU_CMD_SET_ALPHACHANNEL:
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 );
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] )
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];
486 msg_Warn( p_spudec->p_fifo,
487 "ignoring blank alpha palette" );
494 case SPU_CMD_SET_COORDINATES:
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 )
501 p_spu->i_y = GetBits( &p_spudec->bit_stream, 12 );
502 p_spu->i_height = GetBits( &p_spudec->bit_stream, 12 )
509 case SPU_CMD_SET_OFFSETS:
511 /* 06xxxxyyyy (byte offsets) */
512 p_spu->p_sys->pi_offset[0] =
513 GetBits( &p_spudec->bit_stream, 16 ) - 4;
515 p_spu->p_sys->pi_offset[1] =
516 GetBits( &p_spudec->bit_stream, 16 ) - 4;
529 /* xx (unknown command) */
530 msg_Err( p_spudec->p_fifo, "unknown command 0x%.2x",
535 /* We need to check for quit commands here */
536 if( p_spudec->p_fifo->b_die )
541 } while( i_command != SPU_CMD_END );
543 } while( i_index == i_next_seq );
545 /* Check that the next sequence index matches the current one */
546 if( i_next_seq != i_cur_seq )
548 msg_Err( p_spudec->p_fifo, "index mismatch (0x%.4x != 0x%.4x)",
549 i_next_seq, i_cur_seq );
553 if( i_index > p_spudec->i_spu_size )
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 );
560 if( !p_spu->i_start )
562 msg_Err( p_spudec->p_fifo, "no `start display' command" );
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;
572 /* Get rid of padding bytes */
573 switch( p_spudec->i_spu_size - i_index )
575 /* Zero or one padding byte, quite usual */
577 RemoveBits( &p_spudec->bit_stream, 8 );
582 /* More than one padding byte - this is very strange, but
583 * we can deal with it */
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 );
589 while( i_index < p_spudec->i_spu_size )
591 RemoveBits( &p_spudec->bit_stream, 8 );
598 if( b_force_display )
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" );
605 /* Successfully parsed ! */
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 )
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;
625 u16 *p_dest = (u16 *)p_spu->p_sys->p_data;
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;
632 vlc_bool_t b_empty_top = 1,
634 unsigned int i_skipped_top = 0,
635 i_skipped_bottom = 0;
637 /* Colormap statistics */
639 int stats[4]; stats[0] = stats[1] = stats[2] = stats[3] = 0;
641 pi_table[ 0 ] = p_spu->p_sys->pi_offset[ 0 ] << 1;
642 pi_table[ 1 ] = p_spu->p_sys->pi_offset[ 1 ] << 1;
644 for( i_y = 0 ; i_y < i_height ; i_y++ )
646 pi_offset = pi_table + i_id;
648 for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
650 i_code = AddNibble( 0, p_src, pi_offset );
654 i_code = AddNibble( i_code, p_src, pi_offset );
658 i_code = AddNibble( i_code, p_src, pi_offset );
662 i_code = AddNibble( i_code, p_src, pi_offset );
664 if( i_code < 0x0100 )
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 )
670 i_code |= ( i_width - i_x ) << 2;
674 /* We have a boo boo ! */
675 msg_Err( p_spudec->p_fifo, "unknown RLE code "
684 if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
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 );
692 /* Try to find the border color */
693 if( p_spu->p_sys->pi_alpha[ i_code & 0x3 ] != 0x00 )
695 i_border = i_code & 0x3;
696 stats[i_border] += i_code >> 2;
699 if( (i_code >> 2) == i_width
700 && p_spu->p_sys->pi_alpha[ i_code & 0x3 ] == 0x00 )
704 /* This is a blank top line, we skip it */
709 /* We can't be sure the current lines will be skipped,
710 * so we store the code just in case. */
719 /* We got a valid code, store it */
722 /* Valid code means no blank line */
725 i_skipped_bottom = 0;
729 /* Check that we didn't go too far */
732 msg_Err( p_spudec->p_fifo, "i_x overflowed, %i > %i",
737 /* Byte-align the stream */
738 if( *pi_offset & 0x1 )
747 /* We shouldn't get any padding bytes */
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" );
754 /* Skip them just in case */
755 while( i_y < i_height )
757 *p_dest++ = i_width << 2;
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 );
767 /* Crop if necessary */
768 if( i_skipped_top || i_skipped_bottom )
770 p_spu->i_y += i_skipped_top;
771 p_spu->i_height -= i_skipped_top + i_skipped_bottom;
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 );
777 /* Handle color if no palette was found */
778 if( !p_spu->p_sys->b_palette )
780 int i, i_inner = -1, i_shade = -1;
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;
788 /* Find the inner colors */
789 for( i = 0 ; i < 4 && i_inner == -1 ; i++ )
797 for( ; i < 4 && i_shade == -1 ; i++ )
801 if( stats[i] > stats[i_inner] )
813 /* Set the inner color */
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;
821 /* Set the anti-aliasing color */
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;
829 msg_Dbg( p_spudec->p_fifo,
830 "using custom palette (border %i, inner %i, shade %i)",
831 i_border, i_inner, i_shade );
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 )
848 /* Common variables */
852 u8 *p_destptr = (u8 *)p_dest;
853 u16 *p_source = (u16 *)p_spu->p_sys->p_data;
856 int i_len, i_color, i_colprecomp, i_destalpha;
860 int i_xscale, i_yscale, i_width, i_height, i_ytmp, i_yreal, i_ynext;
862 switch( p_vout->output.i_chroma )
864 /* I420 target, no scaling */
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 );
872 /* Draw until we reach the bottom of the subtitle */
873 for( i_y = p_spu->i_height * p_pic->Y_PITCH ;
875 i_y -= p_pic->Y_PITCH )
877 /* Draw until we reach the end of the line */
878 for( i_x = p_spu->i_width ; i_x ; )
880 /* Get the RLE part, then draw the line */
881 i_color = *p_source & 0x3;
882 i_len = *p_source++ >> 2;
884 switch( p_spu->p_sys->pi_alpha[ i_color ] )
890 memset( p_dest - i_x - i_y,
891 p_spu->p_sys->pi_yuv[i_color][0], i_len );
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 ];
902 for ( p_destptr = p_dest - i_x - i_y;
903 p_destptr < p_dest - i_x - i_y + i_len;
906 *p_destptr = ( i_colprecomp +
907 *p_destptr * i_destalpha ) >> 4;
918 /* RV16 target, scaling */
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++ )
925 p_clut16[i_color] = 0x1111
926 * ( (u16)p_spu->p_sys->pi_yuv[i_color][0] >> 4 );
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;
932 i_width = p_spu->i_width * i_xscale;
933 i_height = p_spu->i_height * i_yscale;
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;
940 /* Draw until we reach the bottom of the subtitle */
941 for( i_y = 0 ; i_y < i_height ; )
946 /* Check whether we need to draw one line or more than one */
947 if( i_ytmp + 1 >= ( i_y >> 6 ) )
949 /* Just one line : we precalculate i_y >> 6 */
950 i_yreal = p_pic->p->i_pitch * i_ytmp;
952 /* Draw until we reach the end of the line */
953 for( i_x = i_width ; i_x ; )
955 /* Get the RLE part, then draw the line */
956 i_color = *p_source & 0x3;
958 switch( p_spu->p_sys->pi_alpha[ i_color ] )
961 i_x -= i_xscale * ( *p_source++ >> 2 );
965 i_len = i_xscale * ( *p_source++ >> 2 );
966 memset( p_dest - 2 * ( i_x >> 6 ) + i_yreal,
968 2 * ( ( i_len >> 6 ) + 1 ) );
973 /* FIXME: we should do transparency */
974 i_len = i_xscale * ( *p_source++ >> 2 );
975 memset( p_dest - 2 * ( i_x >> 6 ) + i_yreal,
977 2 * ( ( i_len >> 6 ) + 1 ) );
986 i_yreal = p_pic->p->i_pitch * i_ytmp;
987 i_ynext = p_pic->p->i_pitch * i_y >> 6;
989 /* Draw until we reach the end of the line */
990 for( i_x = i_width ; i_x ; )
992 /* Get the RLE part, then draw as many lines as needed */
993 i_color = *p_source & 0x3;
995 switch( p_spu->p_sys->pi_alpha[ i_color ] )
998 i_x -= i_xscale * ( *p_source++ >> 2 );
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 )
1006 memset( p_dest - 2 * ( i_x >> 6 ) + i_ytmp,
1007 p_clut16[ i_color ],
1008 2 * ( ( i_len >> 6 ) + 1 ) );
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 )
1019 memset( p_dest - 2 * ( i_x >> 6 ) + i_ytmp,
1020 p_clut16[ i_color ],
1021 2 * ( ( i_len >> 6 ) + 1 ) );
1032 /* RV32 target, scaling */
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++ )
1040 p_clut32[i_color] = 0x11111111
1041 * ( (u16)p_spu->p_sys->pi_yuv[i_color][0] >> 4 );
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;
1047 i_width = p_spu->i_width * i_xscale;
1048 i_height = p_spu->i_height * i_yscale;
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;
1055 /* Draw until we reach the bottom of the subtitle */
1056 for( i_y = 0 ; i_y < i_height ; )
1061 /* Check whether we need to draw one line or more than one */
1062 if( i_ytmp + 1 >= ( i_y >> 6 ) )
1064 /* Just one line : we precalculate i_y >> 6 */
1065 i_yreal = p_pic->p->i_pitch * i_ytmp;
1067 /* Draw until we reach the end of the line */
1068 for( i_x = i_width ; i_x ; )
1070 /* Get the RLE part, then draw the line */
1071 i_color = *p_source & 0x3;
1073 switch( p_spu->p_sys->pi_alpha[ i_color ] )
1076 i_x -= i_xscale * ( *p_source++ >> 2 );
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 ) );
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 ) );
1099 i_yreal = p_pic->p->i_pitch * i_ytmp;
1100 i_ynext = p_pic->p->i_pitch * i_y >> 6;
1102 /* Draw until we reach the end of the line */
1103 for( i_x = i_width ; i_x ; )
1105 /* Get the RLE part, then draw as many lines as needed */
1106 i_color = *p_source & 0x3;
1108 switch( p_spu->p_sys->pi_alpha[ i_color ] )
1111 i_x -= i_xscale * ( *p_source++ >> 2 );
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 )
1119 memset( p_dest - 4 * ( i_x >> 6 ) + i_ytmp,
1120 p_clut32[ i_color ],
1121 4 * ( ( i_len >> 6 ) + 1 ) );
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 )
1132 memset( p_dest - 4 * ( i_x >> 6 ) + i_ytmp,
1133 p_clut32[ i_color ],
1134 4 * ( ( i_len >> 6 ) + 1 ) );
1145 /* NVidia overlay, no scaling */
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;
1154 i_y -= p_vout->output.i_width )
1156 /* Draw until we reach the end of the line */
1157 for( i_x = p_spu->i_width ; i_x ; )
1159 /* Get the RLE part, then draw the line */
1160 i_color = *p_source & 0x3;
1162 switch( p_spu->p_sys->pi_alpha[ i_color ] )
1165 i_x -= *p_source++ >> 2;
1169 i_len = *p_source++ >> 2;
1170 for( i_cnt = 0; i_cnt < i_len; i_cnt++ )
1174 memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2,
1175 p_spu->p_sys->pi_yuv[i_color][0], 1);
1177 if (!(i_cnt & 0x01))
1180 memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2 + 1,
1182 memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2 + 3,
1190 /* FIXME: we should do transparency */
1191 i_len = *p_source++ >> 2;
1192 for( i_cnt = 0; i_cnt < i_len; i_cnt++ )
1196 memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2,
1197 p_spu->p_sys->pi_yuv[i_color][0], 1);
1199 if (!(i_cnt & 0x01))
1202 memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2 + 1,
1204 memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2 + 3,
1218 msg_Err( p_vout, "unknown chroma, can't render SPU" );