* ./plugins/spudec/spu_decoder.c: FOURCC_YUY2 subtitles support (for NVidia
[vlc.git] / plugins / spudec / spu_decoder.c
blob911dcc14ca3aaabba57febbb5f5d866e67ab21cd
1 /*****************************************************************************
2 * spu_decoder.c : spu decoder thread
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: spu_decoder.c,v 1.18 2002/05/01 19:18:09 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 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
29 #include <string.h> /* memcpy(), memset() */
31 #include <videolan/vlc.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h> /* getpid() */
35 #endif
37 #ifdef WIN32 /* getpid() for win32 is located in process.h */
38 # include <process.h>
39 #endif
41 #include "video.h"
42 #include "video_output.h"
44 #include "stream_control.h"
45 #include "input_ext-dec.h"
47 #include "spu_decoder.h"
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 * );
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 * );
64 /*****************************************************************************
65 * Capabilities
66 *****************************************************************************/
67 void _M( spudec_getfunctions )( function_list_t * p_function_list )
69 p_function_list->functions.dec.pf_probe = decoder_Probe;
70 p_function_list->functions.dec.pf_run = decoder_Run;
73 /*****************************************************************************
74 * Build configuration tree.
75 *****************************************************************************/
76 MODULE_CONFIG_START
77 MODULE_CONFIG_STOP
79 MODULE_INIT_START
80 SET_DESCRIPTION( _("DVD subtitles decoder module") )
81 ADD_CAPABILITY( DECODER, 50 )
82 MODULE_INIT_STOP
84 MODULE_ACTIVATE_START
85 _M( spudec_getfunctions )( &p_module->p_functions->dec );
86 MODULE_ACTIVATE_STOP
88 MODULE_DEACTIVATE_START
89 MODULE_DEACTIVATE_STOP
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 )
99 return ( *pi_type == DVD_SPU_ES ) ? 0 : -1;
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 )
107 spudec_thread_t * p_spudec;
109 intf_WarnMsg( 3, "spudec: thread launched. Initializing ..." );
111 /* Allocate the memory needed to store the thread's structure */
112 p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) );
114 if ( p_spudec == NULL )
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 );
123 * Initialize the thread properties
125 p_spudec->p_config = p_config;
127 p_spudec->p_fifo = p_config->p_decoder_fifo;
130 * Initialize thread and free configuration
132 p_spudec->p_fifo->b_error = InitThread( p_spudec );
135 * Main loop - it is not executed if an error occured during
136 * initialization
138 while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
140 if( !SyncPacket( p_spudec ) )
142 ParsePacket( p_spudec );
147 * Error loop
149 if( p_spudec->p_fifo->b_error )
151 DecoderError( p_spudec->p_fifo );
154 /* End of thread */
155 EndThread( p_spudec );
157 if( p_spudec->p_fifo->b_error )
159 return( -1 );
162 return( 0 );
166 /* following functions are local */
168 /*****************************************************************************
169 * InitThread: initialize spu decoder thread
170 *****************************************************************************
171 * This function is called from RunThread and performs the second step of the
172 * initialization. It returns 0 on success. Note that the thread's flag are not
173 * modified inside this function.
174 *****************************************************************************/
175 static int InitThread( spudec_thread_t *p_spudec )
177 /* Find an available video output */
178 vlc_mutex_lock( &p_vout_bank->lock );
180 while( p_vout_bank->i_count == 0 )
182 vlc_mutex_unlock( &p_vout_bank->lock );
184 if( p_spudec->p_fifo->b_die || p_spudec->p_fifo->b_error )
186 return( -1 );
189 msleep( VOUT_OUTMEM_SLEEP );
190 vlc_mutex_lock( &p_vout_bank->lock );
193 /* Take the first video output FIXME: take the best one */
194 p_spudec->p_vout = p_vout_bank->pp_vout[ 0 ];
195 vlc_mutex_unlock( &p_vout_bank->lock );
196 InitBitstream( &p_spudec->bit_stream,
197 p_spudec->p_config->p_decoder_fifo, NULL, NULL );
199 /* Mark thread as running and return */
200 return( 0 );
203 /*****************************************************************************
204 * EndThread: thread destruction
205 *****************************************************************************
206 * This function is called when the thread ends after a sucessful
207 * initialization.
208 *****************************************************************************/
209 static void EndThread( spudec_thread_t *p_spudec )
211 free( p_spudec );
214 /*****************************************************************************
215 * SyncPacket: get in sync with the stream
216 *****************************************************************************
217 * This function makes a few sanity checks and returns 0 if it looks like we
218 * are at the beginning of a subpicture packet.
219 *****************************************************************************/
220 static int SyncPacket( spudec_thread_t *p_spudec )
222 /* Re-align the buffer on an 8-bit boundary */
223 RealignBits( &p_spudec->bit_stream );
225 /* The total SPU packet size, often bigger than a PS packet */
226 p_spudec->i_spu_size = GetBits( &p_spudec->bit_stream, 16 );
228 /* The RLE stuff size (remove 4 because we just read 32 bits) */
229 p_spudec->i_rle_size = ShowBits( &p_spudec->bit_stream, 16 ) - 4;
231 /* If the values we got are a bit strange, skip packet */
232 if( !p_spudec->i_spu_size
233 || ( p_spudec->i_rle_size >= p_spudec->i_spu_size ) )
235 return( 1 );
238 RemoveBits( &p_spudec->bit_stream, 16 );
240 return( 0 );
243 /*****************************************************************************
244 * ParsePacket: parse an SPU packet and send it to the video output
245 *****************************************************************************
246 * This function parses the SPU packet and, if valid, sends it to the
247 * video output.
248 *****************************************************************************/
249 static void ParsePacket( spudec_thread_t *p_spudec )
251 subpicture_t * p_spu;
252 u8 * p_src;
253 unsigned int i_offset;
255 intf_WarnMsg( 3, "spudec: trying to gather a 0x%.2x long subtitle",
256 p_spudec->i_spu_size );
258 /* We cannot display a subpicture with no date */
259 if( p_spudec->p_fifo->p_first->i_pts == 0 )
261 intf_WarnMsg( 3, "spudec error: subtitle without a date" );
262 return;
265 /* Allocate the subpicture internal data. */
266 p_spu = vout_CreateSubPicture( p_spudec->p_vout, MEMORY_SUBPICTURE,
267 sizeof( struct subpicture_sys_s )
268 + p_spudec->i_rle_size * 4 );
269 /* Rationale for the "p_spudec->i_rle_size * 4": we are going to
270 * expand the RLE stuff so that we won't need to read nibbles later
271 * on. This will speed things up a lot. Plus, we'll only need to do
272 * this stupid interlacing stuff once. */
274 if( p_spu == NULL )
276 return;
279 /* Fill the p_spu structure */
280 p_spu->pf_render = RenderSPU;
281 p_spu->p_sys->p_data = (u8*)p_spu->p_sys
282 + sizeof( struct subpicture_sys_s );
283 p_spu->p_sys->b_palette = 0;
285 /* Get display time now. If we do it later, we may miss the PTS. */
286 p_spu->p_sys->i_pts = p_spudec->p_fifo->p_first->i_pts;
288 /* Allocate the temporary buffer we will parse */
289 p_src = malloc( p_spudec->i_rle_size );
291 if( p_src == NULL )
293 intf_ErrMsg( "spudec error: could not allocate p_src" );
294 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
295 return;
298 /* Get RLE data */
299 for( i_offset = 0;
300 i_offset + SPU_CHUNK_SIZE < p_spudec->i_rle_size;
301 i_offset += SPU_CHUNK_SIZE )
303 GetChunk( &p_spudec->bit_stream, p_src + i_offset, SPU_CHUNK_SIZE );
305 /* Abort subtitle parsing if we were requested to stop */
306 if( p_spudec->p_fifo->b_die )
308 free( p_src );
309 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
310 return;
314 GetChunk( &p_spudec->bit_stream, p_src + i_offset,
315 p_spudec->i_rle_size - i_offset );
317 #if 0
318 /* Dump the subtitle info */
319 intf_WarnHexDump( 5, p_spu->p_sys->p_data, p_spudec->i_rle_size );
320 #endif
322 /* Getting the control part */
323 if( ParseControlSequences( p_spudec, p_spu ) )
325 /* There was a parse error, delete the subpicture */
326 free( p_src );
327 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
328 return;
331 /* At this point, no more GetBit() command is needed, so we have all
332 * the data we need to tell whether the subtitle is valid. Thus we
333 * try to display it and we ignore b_die. */
335 if( ParseRLE( p_spudec, p_spu, p_src ) )
337 /* There was a parse error, delete the subpicture */
338 free( p_src );
339 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
340 return;
343 intf_WarnMsg( 3, "spudec: total size: 0x%x, RLE offsets: 0x%x 0x%x",
344 p_spudec->i_spu_size,
345 p_spu->p_sys->pi_offset[0], p_spu->p_sys->pi_offset[1] );
347 /* SPU is finished - we can ask the video output to display it */
348 vout_DisplaySubPicture( p_spudec->p_vout, p_spu );
350 /* Clean up */
351 free( p_src );
354 /*****************************************************************************
355 * ParseControlSequences: parse all SPU control sequences
356 *****************************************************************************
357 * This is the most important part in SPU decoding. We get dates, palette
358 * information, coordinates, and so on. For more information on the
359 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
360 *****************************************************************************/
361 static int ParseControlSequences( spudec_thread_t *p_spudec,
362 subpicture_t * p_spu )
364 /* Our current index in the SPU packet */
365 int i_index = p_spudec->i_rle_size + 4;
367 /* The next start-of-control-sequence index and the previous one */
368 int i_next_seq, i_cur_seq;
370 /* Command time and date */
371 u8 i_command;
372 int i_date;
374 /* Dummy stuff */
375 u8 *pi_color;
376 int i;
378 /* XXX: temporary variables */
379 boolean_t b_force_display = 0;
381 /* Initialize the structure */
382 p_spu->i_start = p_spu->i_stop = 0;
383 p_spu->b_ephemer = 0;
387 /* Get the control sequence date */
388 i_date = GetBits( &p_spudec->bit_stream, 16 );
390 /* Next offset */
391 i_cur_seq = i_index;
392 i_next_seq = GetBits( &p_spudec->bit_stream, 16 );
394 /* Skip what we just read */
395 i_index += 4;
399 i_command = GetBits( &p_spudec->bit_stream, 8 );
400 i_index++;
402 switch( i_command )
404 case SPU_CMD_FORCE_DISPLAY:
406 /* 00 (force displaying) */
407 p_spu->i_start = p_spu->p_sys->i_pts + ( i_date * 11000 );
408 b_force_display = 1;
410 break;
412 /* Convert the dates in seconds to PTS values */
413 case SPU_CMD_START_DISPLAY:
415 /* 01 (start displaying) */
416 p_spu->i_start = p_spu->p_sys->i_pts + ( i_date * 11000 );
418 break;
420 case SPU_CMD_STOP_DISPLAY:
422 /* 02 (stop displaying) */
423 p_spu->i_stop = p_spu->p_sys->i_pts + ( i_date * 11000 );
425 break;
427 case SPU_CMD_SET_PALETTE:
429 /* 03xxxx (palette) */
430 if( p_spudec->p_config->p_demux_data &&
431 *(int*)p_spudec->p_config->p_demux_data == 0xBeeF )
433 p_spu->p_sys->b_palette = 1;
434 for( i = 0; i < 4 ; i++ )
436 pi_color = (void*)p_spudec->p_config->p_demux_data
437 + sizeof(int) + 4 * sizeof(u8) *
438 GetBits( &p_spudec->bit_stream, 4 );
439 p_spu->p_sys->pi_yuv[3-i][0] = pi_color[2];
440 p_spu->p_sys->pi_yuv[3-i][1] = pi_color[0];
441 p_spu->p_sys->pi_yuv[3-i][2] = pi_color[1];
444 else
446 RemoveBits( &p_spudec->bit_stream, 16 );
448 i_index += 2;
450 break;
452 case SPU_CMD_SET_ALPHACHANNEL:
454 /* 04xxxx (alpha channel) */
455 for( i = 0; i < 4 ; i++ )
457 p_spu->p_sys->pi_alpha[3-i]
458 = GetBits( &p_spudec->bit_stream, 4 );
460 i_index += 2;
462 break;
464 case SPU_CMD_SET_COORDINATES:
466 /* 05xxxyyyxxxyyy (coordinates) */
467 p_spu->i_x = GetBits( &p_spudec->bit_stream, 12 );
468 p_spu->i_width = GetBits( &p_spudec->bit_stream, 12 )
469 - p_spu->i_x + 1;
471 p_spu->i_y = GetBits( &p_spudec->bit_stream, 12 );
472 p_spu->i_height = GetBits( &p_spudec->bit_stream, 12 )
473 - p_spu->i_y + 1;
475 i_index += 6;
477 break;
479 case SPU_CMD_SET_OFFSETS:
481 /* 06xxxxyyyy (byte offsets) */
482 p_spu->p_sys->pi_offset[0] =
483 GetBits( &p_spudec->bit_stream, 16 ) - 4;
485 p_spu->p_sys->pi_offset[1] =
486 GetBits( &p_spudec->bit_stream, 16 ) - 4;
488 i_index += 4;
490 break;
492 case SPU_CMD_END:
494 /* ff (end) */
495 break;
497 default:
499 /* xx (unknown command) */
500 intf_ErrMsg( "spudec error: unknown command 0x%.2x",
501 i_command );
502 return( 1 );
505 /* We need to check for quit commands here */
506 if( p_spudec->p_fifo->b_die )
508 return( 1 );
511 } while( i_command != SPU_CMD_END );
513 } while( i_index == i_next_seq );
515 /* Check that the next sequence index matches the current one */
516 if( i_next_seq != i_cur_seq )
518 intf_ErrMsg( "spudec error: index mismatch (0x%.4x != 0x%.4x)",
519 i_next_seq, i_cur_seq );
520 return( 1 );
523 if( i_index > p_spudec->i_spu_size )
525 intf_ErrMsg( "spudec error: uh-oh, we went too far (0x%.4x > 0x%.4x)",
526 i_index, p_spudec->i_spu_size );
527 return( 1 );
530 if( !p_spu->i_start )
532 intf_ErrMsg( "spudec error: no `start display' command" );
535 if( !p_spu->i_stop )
537 /* This subtitle will live for 5 seconds or until the next subtitle */
538 p_spu->i_stop = p_spu->i_start + 500 * 11000;
539 p_spu->b_ephemer = 1;
542 /* Get rid of padding bytes */
543 switch( p_spudec->i_spu_size - i_index )
545 /* Zero or one padding byte, quite usual */
546 case 1:
547 RemoveBits( &p_spudec->bit_stream, 8 );
548 i_index++;
549 case 0:
550 break;
552 /* More than one padding byte - this is very strange, but
553 * we can deal with it */
554 default:
555 intf_WarnMsg( 2, "spudec warning: %i padding bytes, we usually "
556 "get 0 or 1 of them",
557 p_spudec->i_spu_size - i_index );
559 while( i_index < p_spudec->i_spu_size )
561 RemoveBits( &p_spudec->bit_stream, 8 );
562 i_index++;
565 break;
568 if( b_force_display )
570 intf_ErrMsg( "spudec: \"force display\" command" );
571 intf_ErrMsg( "spudec: send mail to <sam@zoy.org> if you "
572 "want to help debugging this" );
575 /* Successfully parsed ! */
576 return( 0 );
579 /*****************************************************************************
580 * ParseRLE: parse the RLE part of the subtitle
581 *****************************************************************************
582 * This part parses the subtitle graphical data and stores it in a more
583 * convenient structure for later decoding. For more information on the
584 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
585 *****************************************************************************/
586 static int ParseRLE( spudec_thread_t *p_spudec,
587 subpicture_t * p_spu, u8 * p_src )
589 unsigned int i_code;
591 unsigned int i_width = p_spu->i_width;
592 unsigned int i_height = p_spu->i_height;
593 unsigned int i_x, i_y;
595 u16 *p_dest = (u16 *)p_spu->p_sys->p_data;
597 /* The subtitles are interlaced, we need two offsets */
598 unsigned int i_id = 0; /* Start on the even SPU layer */
599 unsigned int pi_table[ 2 ];
600 unsigned int *pi_offset;
602 boolean_t b_empty_top = 1,
603 b_empty_bottom = 0;
604 unsigned int i_skipped_top = 0,
605 i_skipped_bottom = 0;
607 /* Colormap statistics */
608 int i_border = -1;
609 int stats[4]; stats[0] = stats[1] = stats[2] = stats[3] = 0;
611 pi_table[ 0 ] = p_spu->p_sys->pi_offset[ 0 ] << 1;
612 pi_table[ 1 ] = p_spu->p_sys->pi_offset[ 1 ] << 1;
614 for( i_y = 0 ; i_y < i_height ; i_y++ )
616 pi_offset = pi_table + i_id;
618 for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
620 i_code = AddNibble( 0, p_src, pi_offset );
622 if( i_code < 0x04 )
624 i_code = AddNibble( i_code, p_src, pi_offset );
626 if( i_code < 0x10 )
628 i_code = AddNibble( i_code, p_src, pi_offset );
630 if( i_code < 0x040 )
632 i_code = AddNibble( i_code, p_src, pi_offset );
634 if( i_code < 0x0100 )
636 /* If the 14 first bits are set to 0, then it's a
637 * new line. We emulate it. */
638 if( i_code < 0x0004 )
640 i_code |= ( i_width - i_x ) << 2;
642 else
644 /* We have a boo boo ! */
645 intf_ErrMsg( "spudec error: unknown RLE code "
646 "0x%.4x", i_code );
647 return( 1 );
654 if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
656 intf_ErrMsg( "spudec error: out of bounds, %i at (%i,%i) is "
657 "out of %ix%i",
658 i_code >> 2, i_x, i_y, i_width, i_height );
659 return( 1 );
662 /* Try to find the border color */
663 if( p_spu->p_sys->pi_alpha[ i_code & 0x3 ] != 0x00 )
665 i_border = i_code & 0x3;
666 stats[i_border] += i_code >> 2;
669 if( (i_code >> 2) == i_width
670 && p_spu->p_sys->pi_alpha[ i_code & 0x3 ] == 0x00 )
672 if( b_empty_top )
674 /* This is a blank top line, we skip it */
675 i_skipped_top++;
677 else
679 /* We can't be sure the current lines will be skipped,
680 * so we store the code just in case. */
681 *p_dest++ = i_code;
683 b_empty_bottom = 1;
684 i_skipped_bottom++;
687 else
689 /* We got a valid code, store it */
690 *p_dest++ = i_code;
692 /* Valid code means no blank line */
693 b_empty_top = 0;
694 b_empty_bottom = 0;
695 i_skipped_bottom = 0;
699 /* Check that we didn't go too far */
700 if( i_x > i_width )
702 intf_ErrMsg( "spudec error: i_x overflowed, %i > %i",
703 i_x, i_width );
704 return( 1 );
707 /* Byte-align the stream */
708 if( *pi_offset & 0x1 )
710 (*pi_offset)++;
713 /* Swap fields */
714 i_id = ~i_id & 0x1;
717 /* We shouldn't get any padding bytes */
718 if( i_y < i_height )
720 intf_ErrMsg( "spudec: padding bytes found in RLE sequence" );
721 intf_ErrMsg( "spudec: send mail to <sam@zoy.org> if you "
722 "want to help debugging this" );
724 /* Skip them just in case */
725 while( i_y < i_height )
727 *p_dest++ = i_width << 2;
728 i_y++;
731 return( 1 );
734 intf_WarnMsg( 3, "spudec: valid subtitle, size: %ix%i, position: %i,%i",
735 p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
737 /* Crop if necessary */
738 if( i_skipped_top || i_skipped_bottom )
740 p_spu->i_y += i_skipped_top;
741 p_spu->i_height -= i_skipped_top + i_skipped_bottom;
743 intf_WarnMsg( 3, "spudec: cropped to: %ix%i, position: %i,%i",
744 p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
747 /* Handle color if no palette was found */
748 if( !p_spu->p_sys->b_palette )
750 int i, i_inner = -1, i_shade = -1;
752 /* Set the border color */
753 p_spu->p_sys->pi_yuv[i_border][0] = 0x00;
754 p_spu->p_sys->pi_yuv[i_border][1] = 0x80;
755 p_spu->p_sys->pi_yuv[i_border][2] = 0x80;
756 stats[i_border] = 0;
758 /* Find the inner colors */
759 for( i = 0 ; i < 4 && i_inner == -1 ; i++ )
761 if( stats[i] )
763 i_inner = i;
767 for( ; i < 4 && i_shade == -1 ; i++ )
769 if( stats[i] )
771 if( stats[i] > stats[i_inner] )
773 i_shade = i_inner;
774 i_inner = i;
776 else
778 i_shade = i;
783 /* Set the inner color */
784 if( i_inner != -1 )
786 p_spu->p_sys->pi_yuv[i_inner][0] = 0xff;
787 p_spu->p_sys->pi_yuv[i_inner][1] = 0x80;
788 p_spu->p_sys->pi_yuv[i_inner][2] = 0x80;
791 /* Set the anti-aliasing color */
792 if( i_shade != -1 )
794 p_spu->p_sys->pi_yuv[i_shade][0] = 0x80;
795 p_spu->p_sys->pi_yuv[i_shade][1] = 0x80;
796 p_spu->p_sys->pi_yuv[i_shade][2] = 0x80;
799 intf_WarnMsg( 3, "spudec: using custom palette (border %i, inner %i, "
800 "shade %i)", i_border, i_inner, i_shade );
803 return( 0 );
806 /*****************************************************************************
807 * RenderSPU: draw an SPU on a picture
808 *****************************************************************************
809 * This is a fast implementation of the subpicture drawing code. The data
810 * has been preprocessed once, so we don't need to parse the RLE buffer again
811 * and again. Most sanity checks are already done so that this routine can be
812 * as fast as possible.
813 *****************************************************************************/
814 static void RenderSPU( const vout_thread_t *p_vout, picture_t *p_pic,
815 const subpicture_t *p_spu )
817 /* Common variables */
818 u16 p_clut16[4];
819 u32 p_clut32[4];
820 u8 *p_dest;
821 u16 *p_source = (u16 *)p_spu->p_sys->p_data;
823 int i_x, i_y;
824 int i_len, i_color;
825 u8 i_cnt;
827 /* RGB-specific */
828 int i_xscale, i_yscale, i_width, i_height, i_ytmp, i_yreal, i_ynext;
830 switch( p_vout->output.i_chroma )
832 /* I420 target, no scaling */
833 case FOURCC_I420:
834 case FOURCC_IYUV:
835 case FOURCC_YV12:
837 p_dest = p_pic->Y_PIXELS + p_spu->i_x + p_spu->i_width
838 + p_pic->Y_PITCH * ( p_spu->i_y + p_spu->i_height );
840 /* Draw until we reach the bottom of the subtitle */
841 for( i_y = p_spu->i_height * p_pic->Y_PITCH ;
842 i_y ;
843 i_y -= p_pic->Y_PITCH )
845 /* Draw until we reach the end of the line */
846 for( i_x = p_spu->i_width ; i_x ; )
848 /* Get the RLE part, then draw the line */
849 i_color = *p_source & 0x3;
851 switch( p_spu->p_sys->pi_alpha[ i_color ] )
853 case 0x00:
854 i_x -= *p_source++ >> 2;
855 break;
857 case 0x0f:
858 i_len = *p_source++ >> 2;
859 memset( p_dest - i_x - i_y,
860 p_spu->p_sys->pi_yuv[i_color][0], i_len );
861 i_x -= i_len;
862 break;
864 default:
865 /* FIXME: we should do transparency */
866 i_len = *p_source++ >> 2;
867 memset( p_dest - i_x - i_y,
868 p_spu->p_sys->pi_yuv[i_color][0], i_len );
869 i_x -= i_len;
870 break;
875 break;
877 /* RV16 target, scaling */
878 case FOURCC_RV16:
880 /* FIXME: get this from the DVD */
881 for( i_color = 0; i_color < 4; i_color++ )
883 p_clut16[i_color] = 0x1111
884 * ( (u16)p_spu->p_sys->pi_yuv[i_color][0] >> 4 );
887 i_xscale = ( p_vout->output.i_width << 6 ) / p_vout->render.i_width;
888 i_yscale = ( p_vout->output.i_height << 6 ) / p_vout->render.i_height;
890 i_width = p_spu->i_width * i_xscale;
891 i_height = p_spu->i_height * i_yscale;
893 p_dest = p_pic->p->p_pixels + ( i_width >> 6 ) * 2
894 /* Add the picture coordinates and the SPU coordinates */
895 + ( (p_spu->i_x * i_xscale) >> 6 ) * 2
896 + ( (p_spu->i_y * i_yscale) >> 6 ) * p_vout->output.i_width * 2;
898 /* Draw until we reach the bottom of the subtitle */
899 for( i_y = 0 ; i_y < i_height ; )
901 i_ytmp = i_y >> 6;
902 i_y += i_yscale;
904 /* Check whether we need to draw one line or more than one */
905 if( i_ytmp + 1 >= ( i_y >> 6 ) )
907 /* Just one line : we precalculate i_y >> 6 */
908 i_yreal = p_vout->output.i_width * 2 * i_ytmp;
910 /* Draw until we reach the end of the line */
911 for( i_x = i_width ; i_x ; )
913 /* Get the RLE part, then draw the line */
914 i_color = *p_source & 0x3;
916 switch( p_spu->p_sys->pi_alpha[ i_color ] )
918 case 0x00:
919 i_x -= i_xscale * ( *p_source++ >> 2 );
920 break;
922 case 0x0f:
923 i_len = i_xscale * ( *p_source++ >> 2 );
924 memset( p_dest - 2 * ( i_x >> 6 ) + i_yreal,
925 p_clut16[ i_color ],
926 2 * ( ( i_len >> 6 ) + 1 ) );
927 i_x -= i_len;
928 break;
930 default:
931 /* FIXME: we should do transparency */
932 i_len = i_xscale * ( *p_source++ >> 2 );
933 memset( p_dest - 2 * ( i_x >> 6 ) + i_yreal,
934 p_clut16[ i_color ],
935 2 * ( ( i_len >> 6 ) + 1 ) );
936 i_x -= i_len;
937 break;
942 else
944 i_yreal = p_vout->output.i_width * 2 * i_ytmp;
945 i_ynext = p_vout->output.i_width * 2 * i_y >> 6;
947 /* Draw until we reach the end of the line */
948 for( i_x = i_width ; i_x ; )
950 /* Get the RLE part, then draw as many lines as needed */
951 i_color = *p_source & 0x3;
953 switch( p_spu->p_sys->pi_alpha[ i_color ] )
955 case 0x00:
956 i_x -= i_xscale * ( *p_source++ >> 2 );
957 break;
959 case 0x0f:
960 i_len = i_xscale * ( *p_source++ >> 2 );
961 for( i_ytmp = i_yreal ; i_ytmp < i_ynext ;
962 i_ytmp += p_vout->output.i_width * 2 )
964 memset( p_dest - 2 * ( i_x >> 6 ) + i_ytmp,
965 p_clut16[ i_color ],
966 2 * ( ( i_len >> 6 ) + 1 ) );
968 i_x -= i_len;
969 break;
971 default:
972 /* FIXME: we should do transparency */
973 i_len = i_xscale * ( *p_source++ >> 2 );
974 for( i_ytmp = i_yreal ; i_ytmp < i_ynext ;
975 i_ytmp += p_vout->output.i_width * 2 )
977 memset( p_dest - 2 * ( i_x >> 6 ) + i_ytmp,
978 p_clut16[ i_color ],
979 2 * ( ( i_len >> 6 ) + 1 ) );
981 i_x -= i_len;
982 break;
988 break;
990 /* RV32 target, scaling */
991 case FOURCC_RV24:
992 case FOURCC_RV32:
994 /* XXX: this is a COMPLETE HACK, memcpy is unable to do u32s anyway */
995 /* FIXME: get this from the DVD */
996 for( i_color = 0; i_color < 4; i_color++ )
998 p_clut32[i_color] = 0x11111111
999 * ( (u16)p_spu->p_sys->pi_yuv[i_color][0] >> 4 );
1002 i_xscale = ( p_vout->output.i_width << 6 ) / p_vout->render.i_width;
1003 i_yscale = ( p_vout->output.i_height << 6 ) / p_vout->render.i_height;
1005 i_width = p_spu->i_width * i_xscale;
1006 i_height = p_spu->i_height * i_yscale;
1008 p_dest = p_pic->p->p_pixels + ( i_width >> 6 ) * 4
1009 /* Add the picture coordinates and the SPU coordinates */
1010 + ( (p_spu->i_x * i_xscale) >> 6 ) * 4
1011 + ( (p_spu->i_y * i_yscale) >> 6 ) * p_vout->output.i_width * 4;
1013 /* Draw until we reach the bottom of the subtitle */
1014 for( i_y = 0 ; i_y < i_height ; )
1016 i_ytmp = i_y >> 6;
1017 i_y += i_yscale;
1019 /* Check whether we need to draw one line or more than one */
1020 if( i_ytmp + 1 >= ( i_y >> 6 ) )
1022 /* Just one line : we precalculate i_y >> 6 */
1023 i_yreal = p_vout->output.i_width * 4 * i_ytmp;
1025 /* Draw until we reach the end of the line */
1026 for( i_x = i_width ; i_x ; )
1028 /* Get the RLE part, then draw the line */
1029 i_color = *p_source & 0x3;
1031 switch( p_spu->p_sys->pi_alpha[ i_color ] )
1033 case 0x00:
1034 i_x -= i_xscale * ( *p_source++ >> 2 );
1035 break;
1037 case 0x0f:
1038 i_len = i_xscale * ( *p_source++ >> 2 );
1039 memset( p_dest - 4 * ( i_x >> 6 ) + i_yreal,
1040 p_clut32[ i_color ], 4 * ( ( i_len >> 6 ) + 1 ) );
1041 i_x -= i_len;
1042 break;
1044 default:
1045 /* FIXME: we should do transparency */
1046 i_len = i_xscale * ( *p_source++ >> 2 );
1047 memset( p_dest - 4 * ( i_x >> 6 ) + i_yreal,
1048 p_clut32[ i_color ], 4 * ( ( i_len >> 6 ) + 1 ) );
1049 i_x -= i_len;
1050 break;
1055 else
1057 i_yreal = p_vout->output.i_width * 4 * i_ytmp;
1058 i_ynext = p_vout->output.i_width * 4 * i_y >> 6;
1060 /* Draw until we reach the end of the line */
1061 for( i_x = i_width ; i_x ; )
1063 /* Get the RLE part, then draw as many lines as needed */
1064 i_color = *p_source & 0x3;
1066 switch( p_spu->p_sys->pi_alpha[ i_color ] )
1068 case 0x00:
1069 i_x -= i_xscale * ( *p_source++ >> 2 );
1070 break;
1072 case 0x0f:
1073 i_len = i_xscale * ( *p_source++ >> 2 );
1074 for( i_ytmp = i_yreal ; i_ytmp < i_ynext ;
1075 i_ytmp += p_vout->output.i_width * 4 )
1077 memset( p_dest - 4 * ( i_x >> 6 ) + i_ytmp,
1078 p_clut32[ i_color ],
1079 4 * ( ( i_len >> 6 ) + 1 ) );
1081 i_x -= i_len;
1082 break;
1084 default:
1085 /* FIXME: we should do transparency */
1086 i_len = i_xscale * ( *p_source++ >> 2 );
1087 for( i_ytmp = i_yreal ; i_ytmp < i_ynext ;
1088 i_ytmp += p_vout->output.i_width * 4 )
1090 memset( p_dest - 4 * ( i_x >> 6 ) + i_ytmp,
1091 p_clut32[ i_color ],
1092 4 * ( ( i_len >> 6 ) + 1 ) );
1094 i_x -= i_len;
1095 break;
1101 break;
1103 /* NVidia overlay, no scaling */
1104 case FOURCC_YUY2:
1106 p_dest = p_pic->p->p_pixels +
1107 (p_spu->i_x + p_spu->i_width +
1108 p_vout->output.i_width * ( p_spu->i_y + p_spu->i_height )) * 2;
1109 /* Draw until we reach the bottom of the subtitle */
1110 for( i_y = p_spu->i_height * p_vout->output.i_width;
1111 i_y ;
1112 i_y -= p_vout->output.i_width )
1114 /* Draw until we reach the end of the line */
1115 for( i_x = p_spu->i_width ; i_x ; )
1117 /* Get the RLE part, then draw the line */
1118 i_color = *p_source & 0x3;
1120 switch( p_spu->p_sys->pi_alpha[ i_color ] )
1122 case 0x00:
1123 i_x -= *p_source++ >> 2;
1124 break;
1126 case 0x0f:
1127 i_len = *p_source++ >> 2;
1128 for( i_cnt = 0; i_cnt < i_len; i_cnt++ )
1130 /* draw a pixel */
1131 /* Y */
1132 memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2,
1133 p_spu->p_sys->pi_yuv[i_color][0], 1);
1135 if (!(i_cnt & 0x01))
1137 /* U and V */
1138 memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2 + 1,
1139 0x80, 1);
1140 memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2 + 3,
1141 0x80, 1);
1144 i_x -= i_len;
1145 break;
1147 default:
1148 /* FIXME: we should do transparency */
1149 i_len = *p_source++ >> 2;
1150 for( i_cnt = 0; i_cnt < i_len; i_cnt++ )
1152 /* draw a pixel */
1153 /* Y */
1154 memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2,
1155 p_spu->p_sys->pi_yuv[i_color][0], 1);
1157 if (!(i_cnt & 0x01))
1159 /* U and V */
1160 memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2 + 1,
1161 0x80, 1);
1162 memset( p_dest - i_x * 2 - i_y * 2 + i_cnt * 2 + 3,
1163 0x80, 1);
1166 i_x -= i_len;
1167 break;
1172 break;
1175 default:
1176 intf_ErrMsg( "vout error: unknown chroma, can't render SPU" );
1177 break;