1 /*****************************************************************************
2 * spu_decoder.c : spu decoder thread
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: spu_decoder.c,v 1.45 2001/05/11 15:10:01 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <unistd.h> /* getpid() */
30 #ifdef WIN32 /* getpid() for win32 is located in process.h */
34 #include <stdlib.h> /* malloc(), free() */
35 #include <string.h> /* memcpy(), memset() */
44 #include "stream_control.h"
45 #include "input_ext-dec.h"
48 #include "video_output.h"
50 #include "spu_decoder.h"
52 /*****************************************************************************
54 *****************************************************************************/
55 static int InitThread ( spudec_thread_t * );
56 static void RunThread ( spudec_thread_t * );
57 static void ErrorThread ( spudec_thread_t * );
58 static void EndThread ( spudec_thread_t * );
60 static int SyncPacket ( spudec_thread_t * );
61 static void ParsePacket ( spudec_thread_t * );
62 static int ParseControlSequences( spudec_thread_t *, subpicture_t * );
63 static int ParseRLE ( spudec_thread_t *, subpicture_t *, u8 * );
65 /*****************************************************************************
66 * spudec_CreateThread: create a spu decoder thread
67 *****************************************************************************/
68 vlc_thread_t spudec_CreateThread( vdec_config_t * p_config )
70 spudec_thread_t * p_spudec;
72 /* Allocate the memory needed to store the thread's structure */
73 p_spudec = (spudec_thread_t *)malloc( sizeof(spudec_thread_t) );
75 if ( p_spudec == NULL )
77 intf_ErrMsg( "spudec error: not enough memory "
78 "for spudec_CreateThread() to create the new thread" );
83 * Initialize the thread properties
85 p_spudec->p_config = p_config;
87 p_spudec->p_fifo = p_config->decoder_config.p_decoder_fifo;
89 /* Spawn a video output if there is none */
90 vlc_mutex_lock( &p_vout_bank->lock );
92 if( p_vout_bank->i_count == 0 )
94 intf_WarnMsg( 1, "spudec: no vout present, spawning one" );
96 p_spudec->p_vout = vout_CreateThread( NULL );
98 /* Everything failed */
99 if( p_spudec->p_vout == NULL )
101 intf_Msg( "spudec: can't open vout, aborting" );
102 vlc_mutex_unlock( &p_vout_bank->lock );
108 p_vout_bank->pp_vout[ p_vout_bank->i_count ] = p_spudec->p_vout;
109 p_vout_bank->i_count++;
113 /* Take the first video output FIXME: take the best one */
114 p_spudec->p_vout = p_vout_bank->pp_vout[ 0 ];
117 vlc_mutex_unlock( &p_vout_bank->lock );
119 /* Spawn the spu decoder thread */
120 if ( vlc_thread_create(&p_spudec->thread_id, "spu decoder",
121 (vlc_thread_func_t)RunThread, (void *)p_spudec) )
123 intf_ErrMsg( "spudec error: can't spawn spu decoder thread" );
128 return( p_spudec->thread_id );
131 /* following functions are local */
133 /*****************************************************************************
134 * InitThread: initialize spu decoder thread
135 *****************************************************************************
136 * This function is called from RunThread and performs the second step of the
137 * initialization. It returns 0 on success. Note that the thread's flag are not
138 * modified inside this function.
139 *****************************************************************************/
140 static int InitThread( spudec_thread_t *p_spudec )
142 p_spudec->p_config->decoder_config.pf_init_bit_stream(
143 &p_spudec->bit_stream,
144 p_spudec->p_config->decoder_config.p_decoder_fifo, NULL, NULL );
146 /* Mark thread as running and return */
150 /*****************************************************************************
151 * RunThread: spu decoder thread
152 *****************************************************************************
153 * spu decoder thread. This function only returns when the thread is
155 *****************************************************************************/
156 static void RunThread( spudec_thread_t *p_spudec )
158 intf_WarnMsg( 3, "spudec: spu decoder thread %i spawned", getpid() );
161 * Initialize thread and free configuration
163 p_spudec->p_fifo->b_error = InitThread( p_spudec );
166 * Main loop - it is not executed if an error occured during
169 while( (!p_spudec->p_fifo->b_die) && (!p_spudec->p_fifo->b_error) )
171 if( !SyncPacket( p_spudec ) )
173 ParsePacket( p_spudec );
180 if( p_spudec->p_fifo->b_error )
182 ErrorThread( p_spudec );
186 intf_WarnMsg( 3, "spudec: destroying spu decoder thread %i", getpid() );
187 EndThread( p_spudec );
190 /*****************************************************************************
191 * ErrorThread: RunThread() error loop
192 *****************************************************************************
193 * This function is called when an error occured during thread main's loop. The
194 * thread can still receive feed, but must be ready to terminate as soon as
196 *****************************************************************************/
197 static void ErrorThread( spudec_thread_t *p_spudec )
199 /* We take the lock, because we are going to read/write the start/end
200 * indexes of the decoder fifo */
201 vlc_mutex_lock( &p_spudec->p_fifo->data_lock );
203 /* Wait until a `die' order is sent */
204 while( !p_spudec->p_fifo->b_die )
206 /* Trash all received PES packets */
207 while( !DECODER_FIFO_ISEMPTY(*p_spudec->p_fifo) )
209 p_spudec->p_fifo->pf_delete_pes( p_spudec->p_fifo->p_packets_mgt,
210 DECODER_FIFO_START(*p_spudec->p_fifo) );
211 DECODER_FIFO_INCSTART( *p_spudec->p_fifo );
214 /* Waiting for the input thread to put new PES packets in the fifo */
215 vlc_cond_wait( &p_spudec->p_fifo->data_wait,
216 &p_spudec->p_fifo->data_lock );
219 /* We can release the lock before leaving */
220 vlc_mutex_unlock( &p_spudec->p_fifo->data_lock );
223 /*****************************************************************************
224 * EndThread: thread destruction
225 *****************************************************************************
226 * This function is called when the thread ends after a sucessful
228 *****************************************************************************/
229 static void EndThread( spudec_thread_t *p_spudec )
231 free( p_spudec->p_config );
235 /*****************************************************************************
236 * SyncPacket: get in sync with the stream
237 *****************************************************************************
238 * This function makes a few sanity checks and returns 0 if it looks like we
239 * are at the beginning of a subpicture packet.
240 *****************************************************************************/
241 static int SyncPacket( spudec_thread_t *p_spudec )
243 /* Re-align the buffer on an 8-bit boundary */
244 RealignBits( &p_spudec->bit_stream );
246 /* The total SPU packet size, often bigger than a PS packet */
247 p_spudec->i_spu_size = GetBits( &p_spudec->bit_stream, 16 );
249 /* The RLE stuff size (remove 4 because we just read 32 bits) */
250 p_spudec->i_rle_size = ShowBits( &p_spudec->bit_stream, 16 ) - 4;
252 /* If the values we got are a bit strange, skip packet */
253 if( !p_spudec->i_spu_size
254 || ( p_spudec->i_rle_size >= p_spudec->i_spu_size ) )
259 RemoveBits( &p_spudec->bit_stream, 16 );
264 /*****************************************************************************
265 * ParsePacket: parse an SPU packet and send it to the video output
266 *****************************************************************************
267 * This function parses the SPU packet and, if valid, sends it to the
269 *****************************************************************************/
270 static void ParsePacket( spudec_thread_t *p_spudec )
272 subpicture_t * p_spu;
274 unsigned int i_offset;
276 intf_WarnMsg( 3, "spudec: trying to gather a 0x%.2x long subtitle",
277 p_spudec->i_spu_size );
279 /* We cannot display a subpicture with no date */
280 if( DECODER_FIFO_START(*p_spudec->p_fifo)->i_pts == 0 )
282 intf_WarnMsg( 3, "spudec error: subtitle without a date" );
286 /* Allocate the subpicture internal data. */
287 p_spu = vout_CreateSubPicture( p_spudec->p_vout, DVD_SUBPICTURE,
288 p_spudec->i_rle_size * 4 );
289 /* Rationale for the "p_spudec->i_rle_size * 4": we are going to
290 * expand the RLE stuff so that we won't need to read nibbles later
291 * on. This will speed things up a lot. Plus, we'll only need to do
292 * this stupid interlacing stuff once. */
299 /* Get display time now. If we do it later, we may miss the PTS. */
300 p_spudec->i_pts = DECODER_FIFO_START(*p_spudec->p_fifo)->i_pts;
302 /* Allocate the temporary buffer we will parse */
303 p_src = malloc( p_spudec->i_rle_size );
307 intf_ErrMsg( "spudec error: could not allocate p_src" );
308 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
314 i_offset + SPU_CHUNK_SIZE < p_spudec->i_rle_size;
315 i_offset += SPU_CHUNK_SIZE )
317 GetChunk( &p_spudec->bit_stream, p_src + i_offset, SPU_CHUNK_SIZE );
319 if( p_spudec->p_fifo->b_die )
322 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
327 GetChunk( &p_spudec->bit_stream, p_src + i_offset,
328 p_spudec->i_rle_size - i_offset );
331 /* Dump the subtitle info */
332 intf_WarnHexDump( 5, p_spu->p_data, p_spudec->i_rle_size );
335 /* Getting the control part */
336 if( p_spudec->p_fifo->b_die || ParseControlSequences( p_spudec, p_spu ) )
338 /* There was a parse error, delete the subpicture */
340 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
344 if( ParseRLE( p_spudec, p_spu, p_src ) )
346 /* There was a parse error, delete the subpicture */
348 vout_DestroySubPicture( p_spudec->p_vout, p_spu );
352 intf_WarnMsg( 3, "spudec: total size: 0x%x, RLE offsets: 0x%x 0x%x",
353 p_spudec->i_spu_size,
354 p_spu->type.spu.i_offset[0], p_spu->type.spu.i_offset[1] );
356 /* SPU is finished - we can ask the video output to display it */
357 vout_DisplaySubPicture( p_spudec->p_vout, p_spu );
363 /*****************************************************************************
364 * ParseControlSequences: parse all SPU control sequences
365 *****************************************************************************
366 * This is the most important part in SPU decoding. We get dates, palette
367 * information, coordinates, and so on. For more information on the
368 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
369 *****************************************************************************/
370 static int ParseControlSequences( spudec_thread_t *p_spudec,
371 subpicture_t * p_spu )
373 /* Our current index in the SPU packet */
374 int i_index = p_spudec->i_rle_size + 4;
376 /* The next start-of-control-sequence index and the previous one */
377 int i_next_seq, i_cur_seq;
379 /* Command time and date */
383 /* XXX: temporary variables */
384 boolean_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) */
416 /* Convert the dates in seconds to PTS values */
417 case SPU_CMD_START_DISPLAY:
419 /* 01 (start displaying) */
420 p_spu->i_start = p_spudec->i_pts + ( i_date * 11000 );
424 case SPU_CMD_STOP_DISPLAY:
426 /* 02 (stop displaying) */
427 p_spu->i_stop = p_spudec->i_pts + ( i_date * 11000 );
431 case SPU_CMD_SET_PALETTE:
433 /* 03xxxx (palette) - trashed */
434 RemoveBits( &p_spudec->bit_stream, 16 );
439 case SPU_CMD_SET_ALPHACHANNEL:
441 /* 04xxxx (alpha channel) - trashed */
442 RemoveBits( &p_spudec->bit_stream, 16 );
447 case SPU_CMD_SET_COORDINATES:
449 /* 05xxxyyyxxxyyy (coordinates) */
450 p_spu->i_x = GetBits( &p_spudec->bit_stream, 12 );
451 p_spu->i_width = GetBits( &p_spudec->bit_stream, 12 )
454 p_spu->i_y = GetBits( &p_spudec->bit_stream, 12 );
455 p_spu->i_height = GetBits( &p_spudec->bit_stream, 12 )
462 case SPU_CMD_SET_OFFSETS:
464 /* 06xxxxyyyy (byte offsets) */
465 p_spu->type.spu.i_offset[0] =
466 GetBits( &p_spudec->bit_stream, 16 ) - 4;
468 p_spu->type.spu.i_offset[1] =
469 GetBits( &p_spudec->bit_stream, 16 ) - 4;
482 /* xx (unknown command) */
483 intf_ErrMsg( "spudec error: unknown command 0x%.2x",
488 } while( i_command != SPU_CMD_END );
490 } while( i_index == i_next_seq );
492 /* Check that the next sequence index matches the current one */
493 if( i_next_seq != i_cur_seq )
495 intf_ErrMsg( "spudec error: index mismatch (0x%.4x != 0x%.4x)",
496 i_next_seq, i_cur_seq );
500 if( i_index > p_spudec->i_spu_size )
502 intf_ErrMsg( "spudec error: uh-oh, we went too far (0x%.4x > 0x%.4x)",
503 i_index, p_spudec->i_spu_size );
507 if( !p_spu->i_start )
509 intf_ErrMsg( "spudec error: no `start display' command" );
514 /* This subtitle will live for 5 seconds or until the next subtitle */
515 p_spu->i_stop = p_spu->i_start + 500 * 11000;
516 p_spu->b_ephemer = 1;
519 /* Get rid of padding bytes */
520 switch( p_spudec->i_spu_size - i_index )
522 /* Zero or one padding byte, quite usual */
524 RemoveBits( &p_spudec->bit_stream, 8 );
529 /* More than one padding byte - this is very strange, but
530 * we can deal with it */
532 intf_WarnMsg( 2, "spudec warning: %i padding bytes, we usually "
534 p_spudec->i_spu_size - i_index );
536 while( i_index < p_spudec->i_spu_size )
538 RemoveBits( &p_spudec->bit_stream, 8 );
545 if( b_force_display )
547 intf_ErrMsg( "spudec: \"force display\" command" );
548 intf_ErrMsg( "spudec: send mail to <sam@zoy.org> if you "
549 "want to help debugging this" );
552 /* Successfully parsed ! */
556 /*****************************************************************************
557 * ParseRLE: parse the RLE part of the subtitle
558 *****************************************************************************
559 * This part parses the subtitle graphical data and stores it in a more
560 * convenient structure for later decoding. For more information on the
561 * subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
562 *****************************************************************************/
563 static int ParseRLE( spudec_thread_t *p_spudec,
564 subpicture_t * p_spu, u8 * p_src )
568 unsigned int i_width = p_spu->i_width;
569 unsigned int i_height = p_spu->i_height;
570 unsigned int i_x, i_y;
572 u16 *p_dest = (u16 *)p_spu->p_data;
574 /* The subtitles are interlaced, we need two offsets */
575 unsigned int i_id = 0; /* Start on the even SPU layer */
576 unsigned int pi_table[ 2 ];
577 unsigned int *pi_offset;
579 boolean_t b_empty_top = 1,
581 unsigned int i_skipped_top = 0,
582 i_skipped_bottom = 0;
584 pi_table[ 0 ] = p_spu->type.spu.i_offset[ 0 ] << 1;
585 pi_table[ 1 ] = p_spu->type.spu.i_offset[ 1 ] << 1;
587 for( i_y = 0 ; i_y < i_height ; i_y++ )
589 pi_offset = pi_table + i_id;
591 for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
593 i_code = AddNibble( 0, p_src, pi_offset );
597 i_code = AddNibble( i_code, p_src, pi_offset );
601 i_code = AddNibble( i_code, p_src, pi_offset );
605 i_code = AddNibble( i_code, p_src, pi_offset );
607 if( i_code < 0x0100 )
609 /* If the 14 first bits are set to 0, then it's a
610 * new line. We emulate it. */
611 if( i_code < 0x0004 )
613 i_code |= ( i_width - i_x ) << 2;
617 /* We have a boo boo ! */
618 intf_ErrMsg( "spudec error: unknown RLE code "
627 if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
629 intf_ErrMsg( "spudec error: out of bounds, %i at (%i,%i) is "
631 i_code >> 2, i_x, i_y, i_width, i_height);
635 if( i_code == (i_width << 2) ) /* FIXME: we assume 0 is transp */
639 /* This is a blank top line, we skip it */
644 /* We can't be sure the current lines will be skipped,
645 * so we store the code just in case. */
654 /* We got a valid code, store it */
657 /* Valid code means no blank line */
660 i_skipped_bottom = 0;
664 /* Check that we didn't go too far */
667 intf_ErrMsg( "spudec error: i_x overflowed, %i > %i",
672 /* Byte-align the stream */
673 if( *pi_offset & 0x1 )
682 /* We shouldn't get any padding bytes */
685 intf_ErrMsg( "spudec: padding bytes found in RLE sequence" );
686 intf_ErrMsg( "spudec: send mail to <sam@zoy.org> if you "
687 "want to help debugging this" );
689 /* Skip them just in case */
690 while( i_y < i_height )
692 *p_dest++ = i_width << 2;
699 intf_WarnMsg( 3, "spudec: valid subtitle, size: %ix%i, position: %i,%i",
700 p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
702 /* Crop if necessary */
703 if( i_skipped_top || i_skipped_bottom )
705 p_spu->i_y += i_skipped_top;
706 p_spu->i_height -= i_skipped_top + i_skipped_bottom;
708 intf_WarnMsg( 3, "spudec: cropped to: %ix%i, position: %i,%i",
709 p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );